Skip to content

Animatable: subscriptions to Transitions collection are leaked #12860

Closed
@DmitryZhelnin

Description

@DmitryZhelnin

Describe the bug
The subscription to collection changes made in OnPropertyChangedCore method doesn't take into account whether the control was attached to visual tree or not.

newTransitions.CollectionChanged += TransitionsCollectionChangedHandler;

For example, you have TabControl with two TabItems and the second tab contains a Button. But you never activate the second tab and later detach the whole TabControl from visual tree. In this scenario the subscription is made when the Button is created but since this Button was never attached to visual tree there is no unsubscription when parent control is detached. So the whole parent control can not be garbage collected.

To Reproduce

  • MainWindow.axaml
<Grid RowDefinitions="Auto *">
  <Button Grid.Row="0" Content="Update content" Click="Button_OnClick" />
  <ContentControl Grid.Row="1" Name="contentControl" />
</Grid>
  • MainWindow.axaml.cs
private void Button_OnClick(object sender, RoutedEventArgs e)
{
    contentControl.Content = contentControl.Content is null ? new TabbedContent() : null;
}
  • TabbedContent.axaml
<TabControl>
  <TabItem Header="First Item">
    <TextBlock Text="First" />
  </TabItem>
  <TabItem Header="Second Item">
    <Button Content="Second" />
  </TabItem>
</TabControl>

Steps to reproduce the behavior:

  1. Click Update content button multiple times
  2. Force GC and take memory dump
  3. See count of TabbedContent controls in the memory dump

Expected behavior
Detached instances are garbage collected.

Screenshots
изображение

Desktop (please complete the following information):

  • OS: Windows 10
  • Version 11.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions