MSDN的Dispose()的例子错误? (当设置为null托管引用) [英] MSDN Dispose() example erroneous? (when to set managed references to null)

查看:117
本文介绍了MSDN的Dispose()的例子错误? (当设置为null托管引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN的例子模式来实现Dispose()方法的描绘设置参考释放的托管资源为null( _resource = NULL ),但如果(处置)块外面这样做:

MSDN's example pattern for implementing a Dispose() method depicts setting the reference to a disposed managed resource to null (_resource = null), but does so outside the if (disposing) block:

protected virtual void Dispose(bool disposing)
{
    // If you need thread safety, use a lock around these 
    // operations, as well as in your methods that use the resource.
    if (!_disposed)
    {
        if (disposing) {
            if (_resource != null)
                _resource.Dispose();
                Console.WriteLine("Object disposed.");
        }

        // Indicate that the instance has been disposed.
        _resource = null;
        _disposed = true;   
    }
}



不应该 _resource =空放置在该代码块中?如果以 A调用Dispose(假),就这样 _resource 将是无效和无法进行后续处置! ??

Shouldn't _resource = null be placed inside this code block? If a call to Dispose(false) is made then _resource will be null and unable to be subsequently disposed! ??

当然,的Dispose(假)仅由定稿在运行时调用(实践)。但是,如果 _resource 以前未处置,什么是需要它在这一点当对象(包括 _resource 成员字段)即将被垃圾收集?

Of course, Dispose(false) is only called (in practice) by the runtime during finalization. But if _resource wasn't previously disposed, what is the need to set it to null at this point when the object (including the _resource member field) is about to be garbage collected?

[原题结束]

多阅读后,它将显示设置基准为null是没有必要的,但可能是一个好主意因为如果你有理由相信含有类(一个被布置)可能不会被垃圾收集尽快重的成员对象。

After much reading, it appears setting the reference to null is not necessary, but may be a good idea for "heavy" member objects if you have reason to believe the containing class (the one being disposed) might not be garbage collected soon.

知道该对象的处置不保证该目的已通过消耗代码释放。该释放的对象可保持周围(在收集或以其他方式),用于各种目的,或者只是在错误。我可以想像,使用对象从集合然后将它们的处置,但让他们集合在以后的过程中进行拆除并记录最终状态的应用程序(或者类似的东西...谁知道...)

Know that object disposal is no assurance that the object has been "released" by consuming code. The disposed object might be kept around (in a collection or otherwise) for various purposes, or just in error. I can imagine an application that uses objects from a collection then disposes of them, but keeps them in the collection for a later process to perform removal and log final state (or something like that... who knows...)

结论:


  1. 设置引用到重成员对象为null释放它们
    进行垃圾回收,即使释放的对象不会被释放。

  2. 这是矫枉过正,以清除所有对象的引用。

  3. 因此, _resource = NULL 语句(原题)的位置是不是两个重要的原因:(A)使用它在所有只去想的东西看完上面; (B)在MSDN的例子,它执行为的Dispose(真)的Dispose(假),但后者只有当对象被最终确定,只是要被垃圾收集反正发生!

  1. Setting references to "heavy" member objects to null releases them for garbage collection even if the disposed object is not released.
  2. It is overkill to clear references for all objects.
  3. Hence, placement of the _resource = null statement (the original question) is not important for two reasons: (A) Using it at all is only something to think about after reading the above; (B) In the MSDN example, it executes for both Dispose(true) and Dispose(false), but the latter only occurs when the object is finalized and just about to be garbage collected anyway!

因此,我的偏好将放置 _resource = NULL 最里面的如果块中:

Thus, my preference will be to place _resource = null inside the most inner if block:

if (disposing) {
    if (_resource != null) {
        _resource.Dispose();
        _resource = null;
    }
}

这使所有的 _resource 代码放在一起。 ?另外的想法,任何人

This keeps all the _resource code together. Further thoughts, anyone?

更多阅读:

  • In the Dispose(bool) method implementation, Shouldn't one set members to null?
  • C#: do you need to dispose of objects and set them to null?
  • http://www.codeproject.com/KB/dotnet/idisposable.aspx (Very good, and long!)

推荐答案

将其设置为null被删除引用或指针堆中的位置。这使GC经历,并删除任何有对它的引用没有它不必做太多猜测。 _resource将一些内部使用的对象,需要将其引用清理,所以让我们说,你有内部的关闭套接字,你会关闭套接字或任何其他持久性资源/处置。一旦它的处置将其设置为null,并删除所有引用(应该删除所有引用),因此GC可能做的工作的罚款。我的回答的下半年是那么它种重复一些事情,但我希望你的想法的例子。

Setting it to null is removing the reference or pointer to the location in the heap. This lets the GC go through and remove anything that has no references to it without it having to do much guesswork. _resource would be some internal use object that needs to have its references cleaned up so lets say you have a Socket internal to your close you would close/dispose of that socket or any other persistent resource. Once it's disposed you set it to null and remove all references (should remove all references) so that GC can do it's job fine. The 2nd half of my answer is an example so it kind of repeats some things but I hope you get the idea.

设置为null两次不是什么大不了的事因为没有效果。处置应清理任何资源时真实,即将被垃圾收集反正这只是假的,一旦它(如你所说)。

Setting it to null twice isn't a big deal as there's no effect. Disposing should be true at the time of cleaning up any resources and it's only false once it is (as you said) about to be garbage collected anyway.

这篇关于MSDN的Dispose()的例子错误? (当设置为null托管引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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