C# WeakReference object is NULL in finalizer although still strongly referenced

后端 未结 3 1701
Happy的楠姐
Happy的楠姐 2020-12-17 04:14

Hi I have code here where I don\'t understand why I hit the breakpoint (see comment).

Is this a Microsoft bug of something I don\'t know or I don\'t understand prope

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 05:12

    The WeakReference _weakB is available for garbage collection at the same time as the object a is. You don't have a guarantee of order here, so it could very well be that _weakB is finalized before object a.

    Accessing _weakB in the finalizer of A is dangerous, since you don't know the state of _weakB. I'm guessing that in your case it has been finalized, and that that is causing it to return null for .Target.

提交回复
热议问题