strange WeakReference behavior on Mono

。_饼干妹妹 提交于 2019-11-30 22:26:46

问题


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


回答1:


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 ());
}



回答2:


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

Must ensure Alloc() method not be inline when compile



来源:https://stackoverflow.com/questions/11417283/strange-weakreference-behavior-on-mono

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!