Home » Uncategorized » Dispose()

Dispose()

Today I encountered a very interesting problem : If I have a class that implements the IDisposable interface, when is the Dispose() method called?

Well, I initially thought that the Garbage Collection (GC) calls it, but actually Dispose() is a method like any other and should be called explicitly, by the developer!

Well, than what is the relationship between the CG and Dispose()? First of all, Dispose() is not a destructor, so it can’t free the object’s memory, instead it should be used in order to clean-up the unmanaged resources (because the GC has no knowledge of anything not allocated on the managed heap), like database connections, bitmaps, opened streams or files.

Be careful, because Dispose() is not thread-save and can be called by two threads in the same time on the same object! A very nice answer and more details are given on Chris Lyon’s WebLog

Posted in Uncategorized and tagged as , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.