microsoft / microsoft/microsoft-ui-xaml
ArgumentOutOfRangeException when calling ToList() on ItemsView.SelectedItems
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
I crash reports I see that accessing the [ItemsView.SelectedItems](https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.itemsview.selecteditems) Property crashes for some customers.
This is the stack trace:
```
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, Int32) + 0x4c
at ABI.System.Collections.IList.ToAbiHelper.GetAt(UInt32) + 0x28
at ABI.System.Collections.IList.Do_Abi_GetAt_0(IntPtr, UInt32, IntPtr*) + 0x62
--- End of stack trace from previous location ---
at WinRT.ExceptionHelpers.g__Throw|38_0(Int32) + 0x39
at WinRT.GenericTypeInstantiations.Windows_Foundation_Collections_IIterator_1_object.get_Current(IObjectReference) + 0x73
at ABI.System.Collections.Generic.FromAbiEnumerator`1.MoveNext() + 0xaa
at System.Collections.Generic.List`1..ctor(IEnumerable`1) + 0x78
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1) + 0x3d
at MyApp.MyPage.get_SelectedPics() + 0x33
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.<>c.b__128_0(Object) + 0x1a
at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.b__0() + 0x19
```
### Steps to reproduce the bug
`itemsView.SelectedItems.ToList();`
### Expected behavior
No crash
### Screenshots
_No response_
### NuGet package version
WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003
### Windows version
Windows 11 (24H2): Build 26100
### Additional context
- I am using Native AOT
- Possible related to https://github.com/microsoft/microsoft-ui-xaml/issues/8684 & https://github.com/microsoft/microsoft-ui-xaml/issues/10025
Contributor guide
Research direction
Start with the ItemsView.SelectedItems entry point and the provided stack trace, then compare the possible related issues 8684 and 10025. Reproduce `itemsView.SelectedItems.ToList()` with Windows App SDK 1.6.3 and Native AOT; done means enumerating SelectedItems no longer throws ArgumentOutOfRangeException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100