WPF元素主机内存泄漏

WPF元素主机内存泄漏

本文介绍了WPF元素主机内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用的Windows窗体元素主机一个奇怪的内存泄漏。我有一个主要形式,这将打开另一种形式,一个只有富人就可以了ElementHost控件(其中,在这一点上,没有一个WPF控件的孩子)。只有1主机的形式可以打开。每次我打开窗体,应用程序内存20MB的,这是不是免费的窗体时关闭引发,因此,打开主机形式几次后,我耗尽内存!现在,如果我删除从表单元素主机,内存保持稳定。

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, at this point, does not have a wpf control child).Only 1 host form can be opened.Every time I open the form, the application memory raises by 20Mb, which are not free when the form is closes, so, after opening the host form several times, I run out of memory!.Now, if I remove the element host from the form, the memory remains stable.

我已经运行CLRProfiler和蚂蚁,但我发现,所有的问题所在的单元主机上,而我还没有找到任何解决此。

I have been running CLRProfiler and ANTS, but I have found that all the problem resides on the element host, and I haven't found any workaround for this.

该wpfHost使用了即装即用,从工具栏在WinForm只是拖动。

The wpfHost is used out-of-the-box, just dragged from the toolbar to the winForm.

任何想法,我怎么能解决这个问题?

Any idea how can I solve this?

推荐答案

如果该链接将再次突破,这里是解决方案(复制粘贴)

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

发布者的千戈瑞的上22.10.2010在6:12可能的解决方法:将下面的code到处置或你的控制/表格包含ElementHost控件另一种释放方式。

Posted by KGy on 22.10.2010 at 6:12A 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;
}

这篇关于WPF元素主机内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:28