tags:

views:

101

answers:

1

Does anyone know how to resolve to the memory leak in SL3 with the ChildWindow?

Refer to the code snippet below:

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        var window = new ChildWindow();

        window.Closed += new EventHandler(window_Closed);

        window.Show();
    }

    void window_Closed(object sender, EventArgs e)
    {
        ((ChildWindow)sender).Closed -= new EventHandler(window_Closed);

        WeakReference reference = new WeakReference(sender);

        GC.Collect();

        GC.WaitForPendingFinalizers();

        bool isControlAlive = a.IsAlive;
    }

It is always showing as still "alive" - and when I monitor the iexplore instance in Task Manager - the memory continues to increase everytime I open and close the Child Window.

Please help.

Thanks.

Chris

+1  A: 

There is no official fix yet as far as I know. This page describes the nature of the memory leak:

...[ChildWindow] subscribes to the RootVisual_GotFocus multiple times, but it only unsubscribes it once during close. This causes the ChildWindow to permanently stay in memory attached to the GotFocus event of the RootVisual.

Per the comments section, you can modify the Silverlight Toolkit code as follows to fix the problem:

Modify the ChildWindow_LostFocus function on ChildWindow.cs (Line 731) to subtract the RootVisual_GotFocus listener before adding again:

Application.Current.RootVisual.GotFocus -= this.RootVisual_GotFocus;
Application.Current.RootVisual.GotFocus += this.RootVisual_GotFocus;
Ben Hoffstein
Note that it may be more than just the GotFocus event that is causing a problem. The SizeChanged event also seems to be a culprit..
Ben Hoffstein
I read the comment about the GotFocus event - was not aware of the SizeChanged event. So it's basically an event handler that needs to be unsubscribed from - and it's just a matter of going through the code for the ChildWindow class to find out which event it is?
Chris