AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.36k stars 2.2k forks source link

`ArgumentOutOfRangeException` in `OnDetachedFromLogicalTreeCore` #13497

Open TomEdwardsEnscape opened 10 months ago

TomEdwardsEnscape commented 10 months ago

OnDetachedFromLogicalTreeCore enumerates children of the current control, but doesn't account for the collection of children changing during enumeration.

To Reproduce

  1. Start Control Gallery
  2. Switch to the TabControl tab
  3. Enable "Set TabItem.ContentTemplate"
  4. Close Control Gallery
System.ArgumentOutOfRangeException
  HResult=0x80131502
  Message=Index was out of range. Must be non-negative and less than the size of the collection. Arg_ParamName_Name
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Avalonia.Collections.AvaloniaList`1.get_Item(Int32 index) in Avalonia\src\Avalonia.Base\Collections\AvaloniaList.cs:line 148
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e) in Avalonia\src\Avalonia.Base\StyledElement.cs:line 932
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e) in Avalonia\src\Avalonia.Base\StyledElement.cs:line 934
   ...

Expected behavior

OnDetachedFromLogicalTreeCore should account for the possibility of its children changing during enumeration.

Environment

erri120 commented 7 months ago

Similar to #14437.