IDisposable and managed resources

Possible Duplicate:
Proper use of the IDisposable interface

I have a class that has both managed and unmanaged resources. I am using IDisposable to release un-managed resources. Should I release managed resources in the dispose method? Or I can leave it to GC to release managed resources?


If you have a look at the following documentation you will find the line:

  • Free any disposable resources a type owns in its Dispose method.
  • So in your dispose method you should dispose of managed resources that also implement IDisposable. If an object doesn't implement this, you don't have to dispose of it.


    I would suggest that classes with finalizers (the C# compiler generates finalizers for any classes with destructors) should avoid holding references to any objects that won't be used in finalization. With relatively few exceptions, classes which hold resources that are encapsulated in objects should avoid holding resources that are not so encapsulated. Instead, those resources should be encapsulated into their own objects, so they can be held along with the other resource-containing objects.

    Once that has been taken care of, the proper behavior for any class which owns resources that are encapsulated in other objects is generally to call Dispose on all such resources within its own Dispose method, and not to implement a finalizer (and--for C#--not to have a destructor, which would cause the compiler to generate a finalizer). If the finalizer runs on an object which holds other finalizable objects, each of those objects will usually be in one of three states:

  • The finalizer on that other object will have already run, in which case it's not necessary to do anything to clean it up.
  • The finalizer on that other object will be scheduled to run, in which case it's probably not necessary to do anything to clean it up.
  • Other strong references may exist to that other object, in which case it shouldn't be cleaned up yet.

    Only in the second case would there be any reason to think about doing any cleanup on the other object.

    Note, btw, that the garbage collector should almost never be relied upon to do anything other than free up memory directly consumed by object instances. Deterministic disposal is almost always much better. The only times one should ever deliberately use the garbage collector to clean up resources is when one is going to be creating relatively-low-cost resources which are known to clean themselves up effectively when garbage-collected, and the instances will be widely-enough shared that finding out when the last one leaves scope would otherwise be impractical. Although there is sometimes good justification for abandoning disposable objects, abandoning disposable objects without justification is always a mistake (if it's appropriate to abandon disposable objects, it's appropriate to justify one's reasons for doing so).

    链接地址: http://www.djcxy.com/p/54472.html

    上一篇: 如何在C#中销毁对象

    下一篇: 一次性使用和管理资源