microsoft-ui-xaml: clearing observable collection bound to ListView crashes with ArgumentOutOfRangeException

WinUI Toolkit Bug Report


Name of components or parts of Toolkit: UIElement ListViewRenderer.ListViewTransparent ListViewRenderer TemplatedItemsList ListProxy ObservableCollection

Describe the bug: After calling ObservableCollection.Clear() the execution path travels down into the ABI and crashes there, in a virtual function table call to UpdateLayout, on an instance of ListViewRenderer.ListViewTransparent. The event being processed is OnCollectionChanged, and the NotifyCollectionChangedEventArgsEx.Action is Reset.

Version Info: SDK 7.0.306, RT 7.0.9, VS 17.6.33815.320, MAUI 7.0.92

Additional context:

System.ArgumentOutOfRangeException: This collection cannot work with indices larger than Int32.MaxValue - 1 (0x7FFFFFFF - 1). (Parameter ‘index’) at ABI.System.Collections.IList.ToAbiHelper.EnsureIndexInt32(UInt32 index, Int32 listCapacity) at ABI.System.Collections.IList.ToAbiHelper.GetAt(UInt32 index) at ABI.System.Collections.IList.Vftbl.Do_Abi_GetAt_0(IntPtr thisPtr, UInt32 index, IntPtr* result) — End of stack trace from previous location — at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr) at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr) at ABI.Microsoft.UI.Xaml.IUIElementMethods.UpdateLayout(IObjectReference _obj) at Microsoft.UI.Xaml.UIElement.UpdateLayout() at Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer.<OnCollectionChanged>b__23_0() at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action) at Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at Microsoft.Maui.Controls.Internals.TemplatedItemsList2.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Microsoft.Maui.Controls.Internals.TemplatedItemsList2.OnProxyCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at Microsoft.Maui.Controls.ListProxy.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Microsoft.Maui.Controls.ListProxy.<>c__DisplayClass34_0.<OnCollectionChanged>b__0() at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action) at Microsoft.Maui.Controls.ListProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at Microsoft.Maui.Controls.ListProxy.WeakNotifyProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at TestApp.TestAppUI.<UpdatePage>b__21_0() <— This calls ObservableCollection.Clear(); at Microsoft.Maui.Dispatching.Dispatcher.<>c__DisplayClass9_0.<DispatchImplementation>b__0() at ABI.Microsoft.UI.Dispatching.DispatcherQueueHandler.Do_Abi_Invoke(IntPtr thisPtr)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15 (4 by maintainers)

Most upvoted comments

The same occurs when trying to simple remove an item like this:

public ObservableCollection<string> Items { get; set; } = new ObservableCollection<string>() { "One" };

private void Button_Clicked(object sender, EventArgs e)
{
    Items.RemoveAt(Items.Count - 1);
    Items.Add("new");
}

At the third click on the button, exception occurs.