Note that there are some explanatory texts on larger screens.

plurals
  1. POXAML Button not garbage collected after eliminating references
    primarykey
    data
    text
    <p>I wrote a test program wherein a single <code>Button</code> is defined in XAML as the content of a <code>Window</code>. Upon the window's loading, the <code>Button</code> is programmatically replaced as the window's content, and the field referring to it is also replaced, both by another <code>Button</code> which I created programmatically. I thereafter track both <code>Button</code> objects using weak references, and poll at 1/10th second intervals the <code>IsAlive</code> property of each. Before the first check of <code>IsAlive</code> in the first call to the polling method, I wipe out rooting references to the programmatically-defined <code>Button</code> as well.</p> <p>The expectation in running this code would be that, despite nondeterminism in the timing of C# garbage collection, both <code>Button</code> objects would eventually be reported as garbage collected. Although the programmatically-defined <code>Button</code> shows this behavior, typically within a 1/2 minute, the XAML <code>Button</code> is never collected. I've left the program running for more than ten minutes seeing this behavior.</p> <p>Can anyone tell me why the XAML <code>Button</code> object isn't collected? <strong>In particular, I'd like to know where the garbage collection-blocking reference is</strong>, 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?</p> <p>The program described above is included below for reference.</p> <p>MainWindow.xaml :</p> <pre><code>&lt;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"&gt; &lt;Button Name="btn" /&gt; &lt;/Window&gt; </code></pre> <p>MainWindow.xaml.cs :</p> <pre><code>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)); } } } </code></pre> <p>App.xaml :</p> <pre><code>&lt;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" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload