In the Dispose(bool) method implementation, Shouldn't one set members to null?

后端 未结 3 692
一个人的身影
一个人的身影 2021-01-14 00:46

None of the guides/notes/articles that discuss IDisposable pattern suggest that one should set the internal members to null in the Dispose(bo

相关标签:
3条回答
  • 2021-01-14 01:21

    Releasing any additional references during Dispose is certainly something I try to do, for two reasons:

    • it allows the inner objects to be garbage collected even if the disposed object is still in scope
    • if the inner objects are disposable, it means we only dispose them once even if Dispose() is called repeatedly on the outer object

    For example, I tend to use things like:

    if(someDisposableObject != null)
    {
        someDisposableObject.Dispose();
        someDisposableObject = null;
    }
    (for non-disposable, just set to null)
    someNonDisposableObject = null; // etc
    

    You might also want to set any events to null:

    someEventHandler = null;
    

    This can help minimise the impact if the caller can't fully release their reference (or simply forgets) at the moment. While you should try to release the outer object (for GC), it is relatively easy to accidentally extend the life of the object, for example via a captured variable (anonymous method/lambda), an event, etc.

    If you have a finalizer, then during the GC process there is no benefit doing this, and you shouldn't really call methods on external objects (even Dispose()) - so in short: don't do any of this during a GC sweep.

    0 讨论(0)
  • 2021-01-14 01:23

    Well, generally, it's not going to make a difference. The only place where it will make a difference is when you have a reference to an object on the Large Object Heap, the behavior of which you have seen already).

    There is a good article on the LOH which goes into this in more detail here:

    http://msdn.microsoft.com/en-us/magazine/cc534993.aspx

    0 讨论(0)
  • 2021-01-14 01:27

    Maybe I'm missing your point, but once your object is disposed, the root or 'sub-root' it represented relative to it's members has been detached. It seems like you are thinking of garbage collection like a ref count system (which can be done, but ... usually isn't).

    Instead, think of it as a many-rooted tree, where every object has branches for what it links to. At the end of the day the 'final roots' are statics and anything instantiated from a 'main' loop.

    When the garbage collector runs, the easiest way to think about what it does is to consider that it will walk the list of 'real roots' and apply a 'color' to everything it can 'reach'.

    Now, assumed the collector has access to 'everything', whether it was rooted or not. Anything not colored can be cleaned up.

    Getting back to your original question, when your object is disposed, one assumes (or at least hopes) that no one references it anymore. If this is the case, it is no longer rooted, and so it will not contribute to 'coloring' anything it touches.

    Long story longer - if nulling out members in a Dispose routine is fixing something - I would be you have a different, and real, problem that someone is holding a link to your disposed object, and keeping it 'reachable' when it should not be.

    I apologize for what may be the most over-quote-filled message I've ever written, but I'm sort of abusing standard terms.

    0 讨论(0)
提交回复
热议问题