使用自定义控件的WPF内存泄漏 [英] WPF memory leak with custom control

查看:249
本文介绍了使用自定义控件的WPF内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处置要动态添加到我的应用程序中的控件.在我从其父控件.Remove()回收对象之后,垃圾回收器不会拾取该对象,它具有巨大的位图和几何体私有成员.

我希望能够做这样的事情:

foreach (ScrollItem mylabel in canvas1.Children)
{
    if (mylabel.bRemove == true)
    {
        canvas1.Children.Remove(mylabel);
        mylabel = null;  // or mylabel.Dispose();
    }
}

canvas1不能在UIObjectCollection中包含空项目,因此我不能将其设置为null,如果我只是Remove()它,则垃圾收集器不会收集它.

我试图做类似的事情:

myobj = mylabel;
canvas1.Children.Remove(mylabel);
myobj = null;

但这也不起作用.

解决方案

只要没有对您的对象的引用,GC就会收集它.检查您的应用程序,以获取对您的对象的任何其他引用.通常可以是事件订阅者.您可能还想使用.NET内存探查器,以查看导致对象无法获得GC信号的原因.

I want to dispose of a control I'm dynamically adding to my app. The garbage collector is not picking up the object after I .Remove() it from its parent control, and it has huge bitmaps and geometry private members.

I want to be able to do something like this:

foreach (ScrollItem mylabel in canvas1.Children)
{
    if (mylabel.bRemove == true)
    {
        canvas1.Children.Remove(mylabel);
        mylabel = null;  // or mylabel.Dispose();
    }
}

canvas1 can't have null items in a UIObjectCollection so I can't set it to null, and if I just Remove() it the garbage collector doesn't collect it.

I tried to do something like:

myobj = mylabel;
canvas1.Children.Remove(mylabel);
myobj = null;

but that doesn't work either.

解决方案

As long as there is no references to your object the GC will collect it. Check your app for any additional references to your object. This can typically be event subscribers. You might want to use a .NET memory profiler too see what is preventing your object from beeing GC'ed.

这篇关于使用自定义控件的WPF内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆