----- Original Message -----
From: "Jonni Faiga" <[EMAIL PROTECTED]>

Bills conclusion that SuppressFinalize is not required implies that
Samir Bajaj's statement regarding has Singleton code in
http://msdn.microsoft.com/msdnmag/issues/01/07/patterns/patterns.asp
is that "all you need to do in the C# version is make sure you have a
live reference to the singleton object for as long as it's needed." is
incorrect.

[Mike] No it doesn't.  Finalization and garbage collection are two separate
things.  As long as you have at least one root reference that leads to your
object (directly or indirectly), then your object will not be collected.
That's what Bill said, and it's what Samir said in his article.  In both
cases, a static variable refers to the object.  Since static variables are
considered rooted references that remain valid until the appdomain is
unloaded, your object will never be collected as long as that static
variable refers to you.

Finalization occurs for objects that have already been identified as garbage
by the garbage collector, but that have indicated they need to do a little
cleanup before their memory is reclaimed.  Calling GC.SuppressFinalize just
marks such an the object as no longer requiring it's finalizer method to be
invoked when that object's memory is about to be reclaimed.  It has no
bearing on the earlier decision to reclaim that object's memory or not.  If
an object is never classified as garbage (because there is at least one
rooted reference that leads to it), then the fact that the object needs or
doesn't need to be finalized is a moot point.

The singleton pattern described by Samir uses a static variable to refer to
the object instance that, because statics are rooted references until the
appdomain is unloaded, ensures the object will never be considered for
collection.  This is the same thing Bill was pointing out is happening in
Franklin's code.

-Mike
http://staff.develop.com/woodring
http://www.develop.com/devresources

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to