IDisposable and managed resources [duplicate]

泪湿孤枕 提交于 2020-01-15 12:38:06

问题


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?


回答1:


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.




回答2:


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:

  1. The finalizer on that other object will have already run, in which case it's not necessary to do anything to clean it up.
  2. 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.
  3. 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).



来源:https://stackoverflow.com/questions/7979078/idisposable-and-managed-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!