Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:02:33+00:00 2026-05-26T21:02:33+00:00

I wrote a test program wherein a single Button is defined in XAML as

  • 0

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" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:02:34+00:00Added an answer on May 26, 2026 at 9:02 pm

    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);
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small test program with a sample class containing also self-defined constructor,
I wrote a test program to monitor my Picture folder which points to c:\users[username]\Pictures
I wrote a test program thinking that the address of p1 will be less
I wrote a small program to test accessing a widget parent's slot. Basically, it
Here's a small test program I wrote: #include <iostream> using namespace std; class A
I wrote a simple test program, where I was doing Complex to Complex FT's
I need to test a program I wrote that receives XML as a text
I wrote a program to test my binary tree and when I run it,
Update : I wrote a program to test the memory implications of each of
I wrote a test program that looked like this: #!/usr/bin/python def incrementc(): c =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.