Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak on Popup with ItemsControl and UserControl with empty Binding #15978

Open
Arthur-Camatti opened this issue Jun 10, 2024 · 0 comments

Comments

@Arthur-Camatti
Copy link

Describe the bug

When having a Popup with an ItemsControl that uses a UserControl as DataTemplate, reopening the popup creates new instances of the UserControl and keeps the past ones on memory, never disposing of them. Removing the DataContext="{Binding}" from the xaml solves the leak.

The output console also prints [Binding]Error in binding to 'AvaloniaMemLeak.Controls.DropdownPackageControl'.'DataContext': 'Null value in expression '{empty}' at ''.'(DropdownPackageControl #...) every time for each new UserControl instance, I'm not sure why.

To Reproduce

Create a Window with a Xaml like so

<Button x:Name="PART_footer_selectButton"
        Padding="7"
        Background="Blue"
        HorizontalAlignment="Center"
        VerticalAlignment="Center">
    Press me
</Button>

<Popup x:Name="PART_footer_selectPopup"
       IsLightDismissEnabled="True"
       PlacementTarget="{Binding #PART_footer_selectButton}">
    <ScrollViewer Background="Yellow">
        <ItemsControl ItemsSource="{Binding PackageViewModels, Mode=OneWay}"
                      Padding="1"
                      Margin="1" x:CompileBindings="True">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Spacing="1"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="vm:PackageViewModel">
                    <my:DropdownPackageControl x:DataType="vm:PackageViewModel"
                                               DataContext="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Popup>

and bind the button's Click to the popup's Open on the code-behind. Open and close the popup many times and then take a snapshot of the heap using Visual Studio and search for "DropdownPackageControl". There will be many more instances than the 5 ones expected:
image

If you remove the DataContext="{Binding}" piece from the DataTemplate, the object count does not increase. Not sure if it replaces the old instances or caches them.

Expected behavior

Using DataContext={Binding} on a ItemsControl expected to not keep past UserControl instances on memory and thus cause no memory leak.

Avalonia version

11.0.10

OS

Windows

Additional context

Repro repo: https://github.com/Arthur-Camatti/AvalPopupMemLeak.

Periodic GC.Collect() calls have no effect.
Using .NET 8.0.
Same behavior with Avalonia 11.1.0-beta2.
Same behavior with ReactiveUserControl instead of UserControl.
Haven't tested default Avalonia controls with {Binding}.
Tested with and without x:CompileBindings.
Have confirmed that every Control under the Popup's logical tree is the same instance, except the UserControl (maybe also the ContentPresenter?). I might be mistaken though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants