Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange WeakReference behavior on Mono

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

like image 443
actionresult Avatar asked Jul 10 '12 15:07

actionresult


2 Answers

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 ());
}
like image 136
lupus Avatar answered Oct 21 '22 07:10

lupus


[MethodImpl((MethodImplOptions.NoInlining)]
static WeakReference Alloc ()
{
    return new WeakReference (new object ());
}

Must ensure Alloc() method not be inline when compile

like image 21
Longwei Lai Avatar answered Oct 21 '22 06:10

Longwei Lai