Finalize() vs. Dispose()

Mike_R

Junior Contributor
Joined
Oct 20, 2003
Messages
316
Location
NYC
I'm a bit unclear on the distinction between the Finalize() Sub and the Dispose() Sub in a Form.

What is the difference between the two?

Should I be setting my Class-Level references = Nothing and other cleanup in Finalize() or within Dispose()?

Thanks in advance!

-- Mike
 
The way I understand it, the Finalize method is not as reliable as .Dispose for releasing resources. I also don't think you need to actully set your class-level refs to = Nothing. I think .Dispose takes care of that for you.
 
Yes you seem to be right, I've been doing some reading since I posted this...

VB6-stlye MyObj = Nothing is no longer required really and doesn't even do much, although it can speed up the process of deallocating resources somewhat. Really not needed.

From what I've been reading, Finalize() actually seems to be the proceedure to use, generally, but if you wish to expose a Method that 'cleans up' datastructures that the Object is referencing before deallocation (or at any point) then .Dispose is the way to go. This call results in immediate cleanup, but the Object in question is still present...

Anyway, thank you for your help, and to everyone in this Forum...

:),
Mike
 
Dispose will put your object ready for garbage collector, finalize is more like a method of the class, where u can implement the dispose method... anyway, using dispose will be more effective
 
So is Dispose() called by Finalize()?

If I put my cleanup code in Dispose(), when is it called? (Or do I have to call it explicitly by whatever container is controlling/holding the object?

-- Mike
 
Finalize() is only called when Dispose() isn't, however Finalize() should make a call to Dispose(), passing an argument of false to indicate that only unmanaged resources should be released. One could very well release said resources in Finalize(), however the logic should already be implemented in Dispose(), and further redundancies would serve only to reduce the maintainability of the application. Finalize() should NOT be called directly and it should be marked with the protected member access modifier. Finalize() is invoked by the CLR once the class becomes inaccessible, unless a call to GC.SuppressFinalize() has been made from within the Dispose() method.
 
Back
Top