Description
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.
For example, you have TabControl
with two TabItem
s 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:
- Click
Update content
button multiple times - Force GC and take memory dump
- See count of
TabbedContent
controls in the memory dump
Expected behavior
Detached instances are garbage collected.
Desktop (please complete the following information):
- OS: Windows 10
- Version 11.0.4