I wrote a test program wherein a single Button
is defined in XAML as the content of a Window
. Upon the window's loading, the Button
is programmatically replaced as the window's content, and the field referring to it is also replaced, both by another Button
which I created programmatically. I thereafter track both Button
objects using weak references, and poll at 1/10th second intervals the IsAlive
property of each. Before the first check of IsAlive
in the first call to the polling method, I wipe out rooting references to the programmatically-defined Button
as well.
The expectation in running this code would be that, despite nondeterminism in the timing of C# garbage collection, both Button
objects would eventually be reported as garbage collected. Although the programmatically-defined Button
shows this behavior, typically within a 1/2 minute, the XAML Button
is never collected. I've left the program running for more than ten minutes seeing this behavior.
Can anyone tell me why the XAML Button
object isn't collected? In particular, I'd like to know where the garbage collection-blocking reference is, whether it is in my code or it is in the WPF implementation. Perhaps it's in a XAML loading object. Am I looking at some sort of memory leak?
The program described above is included below for reference.
MainWindow.xaml :
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="150" Loaded="Window_Loaded">
<Button Name="btn" />
</Window>
MainWindow.xaml.cs :
namespace Test {
public partial class MainWindow : System.Windows.Window {
private System.WeakReference wr_xamlBtn, wr_programmaticBtn;
public MainWindow() {
InitializeComponent();
}
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) {
// Immediately upon the window's loading, create a weak reference to the
// button %btn defined in XAML.
wr_xamlBtn = new System.WeakReference(btn);
// Replace references in %btn and this.Content to the XAML button with
// references to a programmatically-defined button. This would be
// expected to free the XAML button for garbage collection.
btn = new System.Windows.Controls.Button();
Content = btn;
// Create a weak reference to the programmatically-defined button, so that
// when (strong) references to it are removed, it will be eligible for
// garbage collection.
wr_programmaticBtn = new System.WeakReference(btn);
// Provides a polling mechanism to see whether either the XAML button or
// the programmatically-defined button has been collected.
var dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += Poll;
dt.Interval = System.TimeSpan.FromMilliseconds(100);
dt.Start();
}
void Poll(object sender, System.EventArgs e) {
// If the programmatically-defined button hasn't had its references
// removed yet, this does so. This makes it eligible for garbage
// collection.
if (btn != null)
Content = btn = null;
// Uses the console to show a timestamp and the state of collection of the
// XAML button and the programmatically-defined button.
System.Console.WriteLine(
string.Format(
"XAML button {0}, Programmatically-defined button {1} @ {2}",
wr_xamlBtn.IsAlive ? "Alive" : "Collected",
wr_programmaticBtn.IsAlive ? "Alive" : "Collected",
System.DateTimeOffset.Now));
}
}
}
App.xaml :
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" />
The button is not collected because it was strongly referenced within the Window namescope:
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);
//...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With