Skip to content

Commit 178d898

Browse files
Marius HanlJeanette Winzenburg
authored andcommitted
8276056: Control.skin.setSkin(Skin) fails to call dispose() on discarded Skin
Co-authored-by: Jeanette Winzenburg <fastegal@openjdk.org> Reviewed-by: aghaisas
1 parent fc6a602 commit 178d898

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

modules/javafx.controls/src/main/java/javafx/scene/control/Control.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -237,19 +237,6 @@ private static Class<?> loadClass(final String className, final Object instance)
237237
// a reference to the old value.
238238
private Skin<?> oldValue;
239239

240-
@Override
241-
//This code is basically a kind of optimization that prevents a Skin that is equal but not instance equal.
242-
//Although it's not kosher from the property perspective (bindings won't pass through set), it should not do any harm.
243-
//But it should be evaluated in the future.
244-
public void set(Skin<?> v) {
245-
if (v == null
246-
? oldValue == null
247-
: oldValue != null && v.getClass().equals(oldValue.getClass()))
248-
return;
249-
250-
super.set(v);
251-
}
252-
253240
@Override protected void invalidated() {
254241
Skin<?> skin = get();
255242
// Collect the name of the currently installed skin class. We do this

modules/javafx.controls/src/test/java/test/javafx/scene/control/skin/SkinMemoryLeakTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -77,6 +77,40 @@ public class SkinMemoryLeakTest {
7777

7878
//--------- tests
7979

80+
/**
81+
* default skin -> set another instance of default skin
82+
*/
83+
@Test
84+
public void testMemoryLeakSameSkinClass() {
85+
installDefaultSkin(control);
86+
Skin<?> skin = control.getSkin();
87+
installDefaultSkin(control);
88+
89+
WeakReference<?> weakRef = new WeakReference<>(skin);
90+
skin = null;
91+
attemptGC(weakRef);
92+
assertNull("Unused Skin must be gc'ed", weakRef.get());
93+
}
94+
95+
@Test
96+
public void testControlChildrenSameSkinClass() {
97+
installDefaultSkin(control);
98+
int childCount = control.getChildrenUnmodifiable().size();
99+
installDefaultSkin(control);
100+
assertEquals("Old skin should dispose children when a new skin is set",
101+
childCount, control.getChildrenUnmodifiable().size());
102+
}
103+
104+
@Test
105+
public void testSetSkinOfSameClass() {
106+
installDefaultSkin(control);
107+
Skin<?> oldSkin = control.getSkin();
108+
installDefaultSkin(control);
109+
Skin<?> newSkin = control.getSkin();
110+
111+
assertNotEquals("New skin was not set", oldSkin, newSkin);
112+
}
113+
80114
/**
81115
* default skin -> set alternative
82116
*/

0 commit comments

Comments
 (0)