Tuesday, November 6, 2007

Resurrection

Just in case if you don't know.

Sometimes it is you may need to resurrects your object.
for example you want the object to clean itself gracefully everytime the object dies.

Here how to do resurrection....

public class MyBaseClass
{
Protected override void Finalize()
{
//TODO: Some Clean up....

GC.ReRegisterForFinalize(this);
}
}

By calling GC.ReRegisterForFinalize method, it will appends the address of the specified object to the end of the finalization queue. When GC detects that this object is unreadchable again, It will queue the object's pointer ob the freachable queue and Finalize method will get called again.

This is example show how to create an object that constantly resurrects itself and never dies...
which usually undesireable. It is far more common to conditionally set a root to a reference the object inside the finalize method.

No comments: