private static void ResurrectionDemo() { Display(0, "\n\nDemo start: Object Resurrection.", +1); // Create a ResurrectionObj and drop it on the floor. ResurrectionInit(); // Force the GC to determine that the object is unreachable. Collect(); WaitForFinalizers(); // You should see the Finalize method called. // However, the ResurrectionObj's Finalize method // resurrects the object keeping it alive. It does this by placing a // reference to the dying-object in Application.ResObjHolder // You can see that ResurrectionObj still exists because // the following line doesn't raise an exception. ResObjHolder.Display("Still alive after Finalize called"); // Prevent the ResurrectionObj object from resurrecting itself again, ResObjHolder.SetResurrection(false); // Now, let's destroy this last reference to the ResurrectionObj ResObjHolder = null; // Force the GC to determine that the object is unreachable. Collect(); WaitForFinalizers(); // You should see the Finalize method called. Display(-1, "Demo stop: Object Resurrection.", 0); }
static public ResurrectObj ResObjHolder; // Defaults to null // These methods demonstrate how the GC supports resurrection. // NOTE: Resurrection is discouraged. private static void ResurrectionInit() { // Create a ResurrectionObj ResurrectObj obj = new ResurrectObj("Resurrection"); // Destroy all strong references to the new ResurrectionObj obj = null; }
public static void ResurrectionDemo() { ResurrectObj obj = new ResurrectObj(); obj = null; GC.Collect(); GC.WaitForPendingFinalizers(); // calling the finalizer again on the resurrected object ResObjHolder = null; GC.Collect(); GC.WaitForPendingFinalizers(); }
static public ResurrectObj ResObjHolder; // Defaults to null // This method demonstrates how the GC supports resurrection. // NOTE: Resurrection is discouraged. private static void ResurrectionDemo() { Display(0, "\n\nDemo start: Object Resurrection.", +1); // Create a ResurrectionObj ResurrectObj obj = new ResurrectObj("Resurrection"); // Destroy all strong references to the new ResurrectionObj obj = null; // Force the GC to determine that the object is unreachable. Collect(); WaitForFinalizers(); // You should see the Finalize method called. // However, the ResurrectionObj's Finalize method // resurrects the object keeping it alive. It does this by placing a // reference to the dying-object in Application.ResObjHolder // You can see that ResurrectionObj still exists because // the following line doesn't raise an exception. ResObjHolder.Display("Still alive after Finalize called"); // Prevent the ResurrectionObj object from resurrecting itself again, ResObjHolder.SetResurrection(false); // Now, let's destroy this last reference to the ResurrectionObj ResObjHolder = null; // Force the GC to determine that the object is unreachable. Collect(); WaitForFinalizers(); // You should see the Finalize method called. Display(-1, "Demo stop: Object Resurrection.", 0); }