Mike_R Posted December 2, 2003 Posted December 2, 2003 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 Quote Posting Guidelines Avatar by Lebb
Mothra Posted December 2, 2003 Posted December 2, 2003 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. Quote Being smarter than you look is always better than looking smarter than you are.
Mike_R Posted December 2, 2003 Author Posted December 2, 2003 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 Quote Posting Guidelines Avatar by Lebb
iebidan Posted December 2, 2003 Posted December 2, 2003 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 Quote Fat kids are harder to kidnap
Mike_R Posted December 2, 2003 Author Posted December 2, 2003 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 Quote Posting Guidelines Avatar by Lebb
*Gurus* Derek Stone Posted December 3, 2003 *Gurus* Posted December 3, 2003 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. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.