Silverlight ChildWindow Memory Leak

余生长醉 提交于 2019-12-22 10:48:44

问题


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:


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;


来源:https://stackoverflow.com/questions/2851951/silverlight-childwindow-memory-leak

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!