Can an object be finalized while still weakly reachable?

Peter Levart peter.levart at gmail.com
Tue May 17 06:49:11 UTC 2016


Hi Martin,


On 05/17/2016 05:19 AM, Martin Buchholz wrote:
> I have some evidence that an object's finalize method can run while a
> weak reference pointing to it is not yet cleared, which surprised me.
>
> E.g.
> class F { protected void finalize() { assert wref.get() != this; } }
> static WeakReference wref = new WeakReference(new F());
>
> If this is a bug, I can try to give y'all a repro recipe.
> If not, we should fix the docs
> """When the weak references to a weakly-reachable object are cleared,
> the object becomes eligible for finalization."""
>
> (It's also quite possible I made a mistake diagnosing this)

What can happen with above code is that you get a NPE from dereferencing 
wref in finlailze(). In case NPE is not thrown and the program 
constructs only a single instance of F then assert should succeed.

It is possible that you made a mistake. Can you post the real code?

Regards, Peter




More information about the core-libs-dev mailing list