Chris
Chris

Reputation: 2168

Silverlight ChildWindow Memory Leak

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

Upvotes: 3

Views: 1871

Answers (1)

Ben Hoffstein
Ben Hoffstein

Reputation: 103355

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;

Upvotes: 4

Related Questions