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

Avalonia 11.0.4 internal error after closing the main application window #12799

Open NGIS72 opened 1 year ago

NGIS72 commented 1 year ago

Avalonia 11.0.4 internal error after closing the main application window.

Message: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException() at Avalonia.Collections.AvaloniaList`1.get_Item(Int32 index) at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e) at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e) at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e) at Avalonia.Controls.TopLevel.HandleClosed() at Avalonia.Controls.WindowBase.HandleClosed() at Avalonia.Controls.Window.HandleClosed() at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)

avalonia_error

timunie commented 1 year ago

please provide a minimal sample to reproduce.

NGIS72 commented 1 year ago

AvaloniaBugApplication1.zip

Procedure of actions Step 1) Switch to Group A, then click on any two elements Step 2) Switch to Group B, then click on any two elements Step 3) Switch to Group C, then click on any two elements Step 4) Close the window

maxkatz6 commented 1 year ago

Not sure why it doesn't work this way, but when I remove unnecessary styles and apply templates directly to the TabControl, it works fine:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="using:AvaloniaBugApplication1"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaBugApplication1.MainWindow"
        Title="AvaloniaBugApplication1">
    <TabControl x:Name="Commands"
                ItemsSource="{Binding ItemsSource}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}" FontWeight="Bold"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding ItemsSource}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <ToggleButton Content="{Binding Title}" Margin="5" Padding="10"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Window>

Makes sense to keep this issue open though

timunie commented 1 year ago

retated to #12753 ?

NGIS72 commented 1 year ago

Not sure why it doesn't work this way, but when I remove unnecessary styles and apply templates directly to the TabControl, it works fine:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="using:AvaloniaBugApplication1"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaBugApplication1.MainWindow"
        Title="AvaloniaBugApplication1">
  <TabControl x:Name="Commands"
              ItemsSource="{Binding ItemsSource}">
      <TabControl.ItemTemplate>
          <DataTemplate>
              <TextBlock Text="{Binding Title}" FontWeight="Bold"/>
          </DataTemplate>
      </TabControl.ItemTemplate>
      <TabControl.ContentTemplate>
          <DataTemplate>
              <ItemsControl ItemsSource="{Binding ItemsSource}">
                  <ItemsControl.ItemsPanel>
                      <ItemsPanelTemplate>
                          <WrapPanel Orientation="Vertical"/>
                      </ItemsPanelTemplate>
                  </ItemsControl.ItemsPanel>
                  <ItemsControl.ItemTemplate>
                      <DataTemplate>
                          <ToggleButton Content="{Binding Title}" Margin="5" Padding="10"/>
                      </DataTemplate>
                  </ItemsControl.ItemTemplate>
              </ItemsControl>
          </DataTemplate>
      </TabControl.ContentTemplate>
  </TabControl>
</Window>

Makes sense to keep this issue open though

Yes, if you go through the TabControl, then everything works fine. But what if you need to apply the style specifically for TabItem? With an example, everything is clear, but in a real project, this option will most likely not work. In any case, this behavior is not normal when, due to the style, an error occurs when closing the application.

NGIS72 commented 1 year ago

retated to #12753 ?

There is another most likely

andrev121 commented 5 months ago

Stuck with this bug too. I don't know how much this would helpful. I Turned off "Just my code" during debugging and found this: image image

Maybe some kind of race condition, don't know, but logicalChildren collections seems to be changed during enumerating

count and logicalChildren.Count are mismatched