XAML Button not garbage collected after eliminating references XAML Button not garbage collected after eliminating references wpf wpf

XAML Button not garbage collected after eliminating references


The button is not collected because it was strongly referenced within the Window namescope:

MemProfiler snapshot

But it shouldn't be recognized as memory leak, because you should reregister your new button within the scope:

//...INameScope scope = NameScope.GetNameScope(this);scope.UnregisterName("btn");btn = new System.Windows.Controls.Button();Content = btn;scope.RegisterName("btn", btn);//...