Testing code that uses WeakReference failed for me using Mono 2.11.3 (SGen) as well as the stable 2.10.8 version. In a simple code like this
object obj = new object();
WeakReference wr = new WeakReference(obj);
Assert.IsTrue(wr.IsAlive);
obj = null;
GC.Collect();
Assert.IsFalse(wr.IsAlive);
the second assert will fail. Adding GC.WaitForPendingFinalizers doesn't help. Is this a bug in Mono or in my head? Thanks
It is not a bug, but an implementation detail where the Mono GC behaves differently from the MS GC. In this case, since you created the object obj in the same stack frame, it happens to be kept alive by the conservative stack scanning code. In real code (as opposed to trivial test cases like this) this is not a problem. If for your particular case it is, I suggest allocating the object and its WeakReference in a separate method:
static WeakReference Alloc ()
{
return new WeakReference (new object ());
}
[MethodImpl((MethodImplOptions.NoInlining)]
static WeakReference Alloc ()
{
return new WeakReference (new object ());
}
Must ensure Alloc()
method not be inline when compile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With