Given SafeHandles these days, it should be rare that you actually implement a 
finalizer - especially if dealing with Windows handles/resources.
From: [email protected] [mailto:[email protected]] On 
Behalf Of Joseph Clark
Sent: Monday, June 14, 2010 11:50 PM
To: ozDotNet
Subject: Re: Implement IDisposable?

Good point, Michael.  I didn't consider the implications of messing with other 
objects in the finalizer.  :)


On Tue, Jun 15, 2010 at 4:28 PM, Michael Minutillo 
<[email protected]<mailto:[email protected]>> wrote:
The "Disposable Pattern" gets discussed a lot but I'm pretty sure you only need 
it when you are holding onto unmanaged resources. From a Finalizer you aren't 
supposed to mess with other objects, I presume this includes objects that the 
framework uses to wrap unmanaged resources for you.

i.e. If you keep a FileStream object then you should dispose of it in your 
Dispose method. If you get to your own finalizer you should not touch the 
FileStream, it has its own finalizer to ensure it gets closed properly. That 
means you don't even need a finalizer. You almost never need a finalizer. You 
pretty much never need to implement the full disposable pattern.

At least, that's how I understand it. If you are holding onto com objects 
directly then it might be a different story but for Files and SqlConnections 
and so on it shouldn't be required.

On Tue, Jun 15, 2010 at 2:02 PM, Joseph Clark 
<[email protected]<mailto:[email protected]>> wrote:
I'm no expert, but I understand there is a "proper" way to implement 
IDisposable to ensure that things always get cleaned up properly.

Implementing IDisposable allows you to cleanly release your resources as soon 
as practical, but relies on the consumer to call the Dispose method properly.  
So, to get around this, you can also define a Finalizer for your class that 
performs your cleanup, in the event that IDisposable is not called.

The problem is that Finalizers have a bunch of caveats attached to them 
(related to performance and memory consumption) and thus shouldn't be defined 
unless you really, really need them.

So, the trick is to call GC.SuppressFinalize in your Dispose method, which 
stops the Finalizer from running needlessly (I don't know about C#, but if you 
implement IDisposable in VB.NET<http://VB.NET>, the IDE automatically injects a 
correct implementation of this pattern for you).

I did a quick Google search and found a codeproject article which looks 
accurate enough and covers it in more detail: 
http://www.codeproject.com/KB/cs/idisposable.aspx

Hope this helps.  My knowledge of finalizers is only theory-based.

Joe.



On Tue, Jun 15, 2010 at 3:34 PM, Bec Carter 
<[email protected]<mailto:[email protected]>> wrote:
Hi!

I have a class which uses temporary files to do its stuff. What do you
recommend for ensuring things get cleaned up when things go bad?
IDisposable? Or perhaps a big try/finally inside the class functions?

Cheers  --Bec--



--
Michael M. Minutillo
Indiscriminate Information Sponge
Blog: http://wolfbyte-net.blogspot.com

Reply via email to