Information Technology Reference
In-Depth Information
This brings me to the most important recommendation for any method
associated with disposal or cleanup: You should be releasing resources only.
Do not perform any other processing during a dispose method. You can
introduce serious complications to object lifetimes by performing other
processing in your Dispose or finalize methods. Objects are born when you
construct them, and they die when the Garbage Collector reclaims them.
Yo u c a n c o n s i d e r t h e m c o m a t o s e w h e n y o u r p r o g r a m c a n n o l o n g e r a c c e s s
them. If you can't reach an object, you can't call any of its methods. For all
intents and purposes, it is dead. But objects that have finalizers get to breathe
a last breath before they are declared dead. Finalizers should do nothing
but clean up unmanaged resources. If a finalizer somehow makes an object
reachable again, it has been resurrected. It's alive and not well, even though
it has awoken from a comatose state. Here's an obvious example:
public class BadClass
{
// Store a reference to a global object:
private static readonly List < BadClass > finalizedList =
new List < BadClass >();
private string msg;
public BadClass( string msg)
{
// cache the reference:
msg = ( string )msg.Clone();
}
~BadClass()
{
// Add this object to the list.
// This object is reachable, no
// longer garbage. It's Back!
finalizedList.Add( this );
}
}
When a BadClass object executes its finalizer, it puts a reference to itself on
a global list. It has just made itself reachable. It's alive again! The number
of problems you've just introduced would make anyone cringe. The object
has been finalized, so the Garbage Collector now believes there is no need
 
Search WWH ::




Custom Search