I’m sure many of you have heard the term “object resurrection” with respect to the GC. It’s an interesting (but not very useful) way to illustrate object lifetimes and the role of finalization versus garbage collection. Basically, it’s a way to reference an object that has been finalized.
Here’s a rough description of how object resurrection can occur:
Here’s a code sample to illustrate:
public class ResurrectedObj
{
~ResurrectedObj()
// this will resurrect the object by assigning
// it to the static reference
resurrectedReference = this;
}
public static ResurrectedObj resurrectedReference = null;
public static void Main()
ResurrectedObj liveReference = new ResurrectedObj();
liveReference = null;
GC.Collect();
// liveReference is now dead and the object is put on the
// freachable queue
GC.WaitForPendingFinalizers();
// at this point, the object previously referenced
// by liveReference is held alive by resurrectedReference
Unfortunately there are several implications to object resurrection which may not be immediately obvious. For these reasons we strongly recommend against resurrecting objects.