Here's an academic question about object finalization and collection in C#/.NET. Background reading is section 3.9 of the C# language spec, Automatic Memory Management.
When there are no explicit references to an object, it may become garbage collected. It becomes "eligible for destruction". At some point in the future (e.g. if you force garbage collection), the object's destructor will be run.
In the destructor, if you save a reference to the object, the object will be finalized, but will not be eligible for collection. This can lead to an object being in a state where it has been finalized, but not collected. Sec 3.9 of the spec has an example of this.
At this point, the object is really still alive, since it has not yet been garbage collected. However, a WeakReference referring to the object reports an IsAlive value of false, indicating that the object has been collected.
The core question is this--what is the IsAlive property really reporting? We know that we can't trust a value of true for this property, because the value can become false shortly after you read it. But a value of false is trustworthy and is meant to indicate (according to documentation) that the object has been garbage collected. So what is the IsAlive property telling us in this case? Not strictly whether the object has been garbage collected, since we believe that the object is in a finalized-but-not-collected state.
Here's a sample to show the behavior.
public class Dog
{
public static Dog KeepDogRef;
public string Name { get; set; }
public Dog(string name)
{
Name = name;
}
~Dog()
{
Console.WriteLine("Dog destructor for " + Name + " called");
Dog.KeepDogRef = this;
}
public void Bark()
{
Console.WriteLine(Name + " : Woof");
}
}
And code for main program. If you run the code, you'll see that the original WeakReference reports IsAlive as false, even after we reconstitute the object.
static void Main()
{
Dog dog = new Dog("Bowser");
WeakReference dogRef = new WeakReference(dog);
// Unref Bowser, now eligible for destruction
dog = null;
GC.Collect();
GC.WaitForPendingFinalizers();
// Bowser no longer alive
Console.WriteLine(string.Format("Object still alive: {0}", dogRef.IsAlive));
// Bowser alive again
Dog newRef = Dog.KeepDogRef;
newRef.Bark();
}
}
If you read all of the documentation for
WeakReference
, it's clear that there is more than one type of weak reference available. The default is to produce a short weak reference. But you can also create long weak references which specifically account for resurrection scenarios.From the documentation for
TrackResurrection
:So I'd say you have to understand this part of weak references before interpreting the
IsAlive
property.