我是否需要调用Dispose()对管理对象? [英] Do I need to call Dispose() on managed objects?

查看:114
本文介绍了我是否需要调用Dispose()对管理对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法相信我仍然感到困惑这一点,但无论如何,让我们终于打钉:

I can't believe I'm still confused about this but, any way, lets finally nail it:

我有一个重写的OnPaint做一些绘图类。为了加快速度,我创建了钢笔,画笔等手之前,在构造函数中,这样的OnPaint并不需要不断创造和处理他们。

I have a class that overrides OnPaint to do some drawing. To speed things up, I create the pens, brushes etc before hand, in the constructor, so that OnPaint does not need to keep creating and disposing them.

现在,我要确保我总是处理这样的对象,但我有感觉,我不需要,因为尽管他们实现IDisposable的事实,他们管理的对象。

Now, I make sure that I always dispose of such objects, but I have the feeling I don't need to because, despite the fact they implement IDisposable, they're managed objects.

这是正确的?

感谢所有的答案,这个问题肯定已经钉。
我很高兴,我一直在使用'使用',所以,我并不需要经过所有的code检查了警惕。我只是想明确的是,我不是一个毫无意义的用户。

Thanks for all the answers, the issue has certainly been nailed.
I'm glad I've been vigilant in always using 'using' so that I don't need to go through all my code checking. I just wanted to be clear that I wasn't being a pointless user.

顺便说一句,我也有一个奇怪的现象,近日,在这里我不得不更换一个使用块和手动调用处理!我会来挖了出来,并创建一个新的问题。

As an aside, I did have a strange situation, recently, where I had to replace a using block and manually call dispose! I'll dig that out and create a new question.

推荐答案

这是的没有的正确。您需要处置的实施的IDisposable 的对象。这就是为什么他们实施的IDisposable - 指定它们封装(直接或间接)非托管资源的事实

It is not correct. You need to dispose objects that implement IDisposable. That is why they implement IDisposable - to specify the fact that they wrap (directly or indirectly) unmanaged resources.

在这种情况下,非托管资源是GDI手柄,如果你不处理它们时,你实际上是和他们做,你会泄漏的句柄。现在,这些的尤其的对象具有的终结的,这将导致当在GC踢,但你不知道何时会发生的资源被释放。它可能是从现在开始10秒后,它可能是从现在是10天;如果你的应用程序没有产生足够的内存pressure使GC踢和运行这些刷/笔/字体的终结/等等,你可以最终饿死的GDI资源操作系统之前,GC以往任何时候都意识到什么是怎么回事。

In this case, the unmanaged resource is a GDI handle, and if you fail to dispose them when you are actually done with them, you will leak those handles. Now these particular objects have finalizers that will cause the resources to be released when the GC kicks in, but you have no way of knowing when that will happen. It might be 10 seconds from now, it might be 10 days from now; if your application does not generate sufficient memory pressure to cause the GC to kick in and run the finalizers on those brushes/pens/fonts/etc., you can end up starving the OS of GDI resources before the GC ever realizes what's going on.

此外,你不能保证每个非托管的包装实际上实现了一个终结。 .NET框架本身是pretty的该班实施意识的IDisposable 正确的格式,但它是完全可能的一些的的类,有一个破碎的实现,不包括终结,因此不收拾妥当,除非处置被称为明确就可以了。在一般情况下,的IDisposable 的目的是,你不应该知道或关心的具体实施细则;相反,如果是一次性的,那么你处理它,期。

Additionally, you have no guarantee that every unmanaged wrapper actually implements a finalizer. The .NET Framework itself is pretty consistent in the sense that classes implementing IDisposable implement it with the correct pattern, but it is completely possible for some other class to have a broken implementation that does not include a finalizer and therefore does not clean up properly unless Dispose is called explicitly on it. In general, the purpose of IDisposable is that you are not supposed to know or care about the specific implementation details; rather, if it's disposable, then you dispose of it, period.

这个故事的寓意:务必正确处置的IDisposable 的对象。如果您的类拥有的对象是的IDisposable ,那么就应该实施的IDisposable 本身。

Moral of the story: Always dispose IDisposable objects. If your class "owns" objects that are IDisposable, then it should implement IDisposable itself.

这篇关于我是否需要调用Dispose()对管理对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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