Sami said: > The thing with relying on the finalizer to release an unmanaged resource > is that by the time the finalizer runs it might be already too late
Hopefully I did not give the impression that I was advocating that developers depend upon the finalizer for clean up of unmanaged resources. That is *not at all* what I intended. If an unhandled exception occurs before an object has an opportunity to clean up its unmanaged resources the finalizer is there as a safety net to ensure that the resources do eventually get cleaned up (albeit late). It is better to be late with respect to cleaning up resources than to never clean them up. The finalizer also catches the circumstance where a developer doesn't call Dispose when they were supposed to (albeit late as well). --Paul Mehner -----Original Message----- From: Unmoderated discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Sami Vaaraniemi Sent: Wednesday, January 26, 2005 1:16 PM To: [email protected] Subject: Re: [ADVANCED-DOTNET] Disposing of objects > I respectfully disagree that 3 is the most important option. The reason for > the finalizer is to ensure that unmanaged resources get cleaned up even if > the developer using the object forgets or is too lazy to call Dispose and/or > an exception in his or her code causes the calling of an object's Dispose > method to not occur. The finalizer will clean up the unmanaged resources > when the object itself is destroyed. > > The fact is that very few classes need any dispose pattern at all. Those > classes that do need a dispose code pattern need to use option 2. > > --Paul Mehner > The thing with relying on the finalizer to release an unmanaged resource is that by the time the finalizer runs it might be already too late. If the resource is scarce the system might have failed already before the finalizer runs. So rather than have the finalizer silently release the unmanaged resource, I'd fail an assertion or log it as an error somewhere (and release the resource if in release mode). This way, hopefully, the programmer is alerted to the leak problem and knows to fix it. Regards, Sami =================================== This list is hosted by DevelopMentorR http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor� http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
