WPF element host memory leak

后端 未结 3 1329
暖寄归人
暖寄归人 2021-02-04 17:35

I\'m having a strange memory leak using element host on windows forms. I have a main form, which opens another form, the one that only haves the elementhost control on it (which

3条回答
  •  礼貌的吻别
    2021-02-04 18:11

    if the link will break again, here is solution (copy-pasted)

    Posted by KGy on 22.10.2010 at 6:12 A possible workaround: Put the following code into the Dispose or another release method of your control/form that contains the ElementHost control.

    if (elementHost != null)
    {
        FrameworkElement fe = elementHost.Child as FrameworkElement;
        if (fe != null)
        {
            // Memory leak workaround: elementHost.Child.SizeChanged -= elementHost.childFrameworkElement_SizeChanged;
            SizeChangedEventHandler handler = (SizeChangedEventHandler)Delegate.CreateDelegate(typeof(SizeChangedEventHandler), elementHost, "childFrameworkElement_SizeChanged");
            fe.SizeChanged -= handler;
        }
        elementHost.Child = null;
        base.Dispose(disposing);
        elementHost.Dispose();
        elementHost.Parent = null;
        elementHost = null;
    }
    

提交回复
热议问题