Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
585
How to close a xamDialogWindow without a memory leak
posted

I am opening a modal xamDialogWindow in several places in my application (the main window and child windows). By default, when you click the close button, the window only gets hidden. That's no good in this situation, because these windows could be opened and closed lots of times and I don't want to reuse the existing one. So to make sure it's unloaded properly, I am handling the WindowStateChanged event and removing it from the parent grid, like so:

private void XamDialogWindow_WindowStateChanged(object sender, WindowStateChangedEventArgs e)
{
    if (e.NewWindowState == Infragistics.Controls.Interactions.WindowState.Hidden)
    {
        Panel p = VisualTreeHelper.GetParent(this) as Panel;
        if (p != null)
        {
            p.Children.Remove(this);
        }
                
    }
}

I'm not sure if this is the correct way to do it, but on the surface, it appears to work fine.

However, the problem that arises is that your internal DialogManager class holds on to references to the container, and this is only cleaned up in the Unloaded event of the xamDialogWindow. If I've already removed the window from it's parent grid then the cleanup code is unable to work out how to properly clean it up, and you get stuck with references to the child windows and so they won't get garabage collected even after they're closed. 

I've attached some sample code which demonstrates this problem. Attaching a memory profilier, or looking at DialogManager in the debugger will show that every ChildWindow which has had a DialogWindow opened inside it will still be hanging around even when closed.

So, the question is, how can I close (not just hide) the xamDialogWindow so that it, and it's parent container, can be garbage collected?

Thanks,

Richard

XamDialogWindowLeakTest.zip
Parents
  • 3255
    posted

    Hello,

    Looks like there is already a development issue logged on this behaviour.  I will create a case and link your case to this development issue, so that you will be notified when it released.

     The development issue number is 75486.

Reply Children