Details
-
Bug
-
Resolution: Unresolved
-
P5
-
jfx16
Description
the brittle pattern analysed in review (by Kevin, https://github.com/openjdk/jfx/pull/409/commits/da0820a9d08ac1a3acbd8b594432bc425a206433#r614171861) :
WeakReference<SomeClass> ref = new WeakReference<>(new SomeClass());
ref.get().doStuff();
attemptGC();
citing Kevin: "This is fragile. It is possible, although unlikely, that the referent could be GCed between its construction in the previous statement (after which it goes out of scope), and this statement. If this rare event happened, it would cause an NPE here. I recommend to keep a local reference to the referent and then set it to null after this (and before the attemptGC)."
The pattern is used elsewhere, f.i. BehaviorCleanupTest, ListViewTest, TabPaneTest, SkinCleanupTest, SkinLabeledCleanupTest, SkinMemoryLeakTest and should be fixed as suggested, that is
SomeClass some = new SomeClass();
WeakReference<SomeClass> ref = new WeakReference<>(some);
some.doStuff(); // could also be moved up a line now
some = null;
attemptGC();
WeakReference<SomeClass> ref = new WeakReference<>(new SomeClass());
ref.get().doStuff();
attemptGC();
citing Kevin: "This is fragile. It is possible, although unlikely, that the referent could be GCed between its construction in the previous statement (after which it goes out of scope), and this statement. If this rare event happened, it would cause an NPE here. I recommend to keep a local reference to the referent and then set it to null after this (and before the attemptGC)."
The pattern is used elsewhere, f.i. BehaviorCleanupTest, ListViewTest, TabPaneTest, SkinCleanupTest, SkinLabeledCleanupTest, SkinMemoryLeakTest and should be fixed as suggested, that is
SomeClass some = new SomeClass();
WeakReference<SomeClass> ref = new WeakReference<>(some);
some.doStuff(); // could also be moved up a line now
some = null;
attemptGC();