David - can you qualify 'these days' for your 3 points with either Framework
version (3.0, 3.5, 4.0 ?) - or is it for CLR 4 only? 

 

  _____  

Ian Thomas
Victoria Park, Western Australia

  _____  

From: [email protected] [mailto:[email protected]]
On Behalf Of David Kean
Sent: Saturday, February 04, 2012 12:12 AM
To: ozDotNet
Subject: RE: Dispose and Finalizer in 4.0

 

No one these days, apart from my team (ie CLR), should be implementing
finalizers:

 

1)      If your class does not own a managed disposable resource, then don't
implement dispose.

2)      If your class owns a managed disposable resource, then only
implement Dispose. 

3)      If your class owns a native resource - wrap it in a SafeHandle
(create a new class if there's isn't an appropriate one), and only implement
Dispose.

 

You should never have to implement a finalizer, except in very rare
situations.

 

From: [email protected] [mailto:[email protected]]
On Behalf Of Jano Petras
Sent: Friday, February 03, 2012 12:12 AM
To: ozDotNet
Subject: Re: Dispose and Finalizer in 4.0

 

Hi David,

Looking here:
http://msdn.microsoft.com/en-us/library/b1yfkh5e%28v=vs.100%29.aspx

Finalize is called by GC when there are no references to the instance. 

Dispose is explicit way to clean up those resources before GC kicks in. 

If dispose was invoked, there is no reason for Finalize to do anything -
hence GS.SuppressFinalize

If dispose was not invoked, the Finalize will be invoked (at some point) by
GC. This is the Dispose(false) situation, where only unmanaged resources are
released. The reason for this is that managed ones will be cleaned up by GC
as well (in the same way it is cleaning up this current instance). 

RE: your question: Should I be implementing a ~Finalize in the code as well
as a Dispose method in 4.0?  

Yes, because you can never say with 100% certainty that every developer that
will touch the code will always call Dispose on the instance. The finalizer
is here to be on the safe side :)


Cheers,
jano



On 2 February 2012 13:03, David Rhys Jones <[email protected]> wrote:

Hi guys, 

 I've just had an interesting conversation with one of the other guys here
concerning the Dispose pattern. 

 Should I be implementing a ~Finalize in the code as well as a Dispose
method in 4.0?  

points. 
   1. No unmanaged resources in the code at all, 
   2. the virtual dispose method has a   if(disposing)  wrapped around the
dispose code.
    
 My point of view is that the objects in question, will never have the
finalizer called because of the GC.Suppress and the finalizer if it is
called, doesn't do anything because 
of the Dispose(false). 

Opinions?

Thanks


Davy,


The US Congress voted Pizza sauce a vegetable. Don't even try to convince me
of anything in the states is sane any more! 

 

Reply via email to