15

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

actionresult
  • 399
  • 2
  • 8
  • 1
    If it is a bug in your head you can remote debug by attaching PsychicDbg, but terminating the session may prove fatal. – Polyfun Jul 10 '12 at 16:13
  • 4
    Relevant: [GC.Collect\(\) CLR<>Mono difference.](http://mono.1490590.n4.nabble.com/GC-Collect-CLR-lt-gt-Mono-difference-td1536244.html) I lose comprehension about 2/3 of the way down :) – AakashM Jul 10 '12 at 16:14

2 Answers2

17

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 ());
}
Jb Evain
  • 17,319
  • 2
  • 67
  • 67
lupus
  • 3,963
  • 1
  • 18
  • 13
6
[MethodImpl((MethodImplOptions.NoInlining)]
static WeakReference Alloc ()
{
    return new WeakReference (new object ());
}

Must ensure Alloc() method not be inline when compile

Longwei Lai
  • 61
  • 1
  • 2