Skip to content

Commit 1c54e61

Browse files
FlorianKirmaierkevinrushforth
authored andcommitted
8241840: Memoryleak: Closed focused Stages are not collected with Monocle.
Reviewed-by: arapte, kcr
1 parent f50d218 commit 1c54e61

File tree

4 files changed

+211
-1
lines changed

4 files changed

+211
-1
lines changed

modules/javafx.graphics/src/main/java/com/sun/glass/ui/monocle/MonocleWindowManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ boolean maximizeWindow(MonocleWindow window) {
121121

122122
boolean requestFocus(MonocleWindow window) {
123123
int index = getWindowIndex(window);
124-
if (index != -1) {
124+
if (index != -1 && window.isVisible()) {
125125
focusedWindow = window;
126126
window.notifyFocus(WindowEvent.FOCUS_GAINED);
127127
return true;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package test.javafx.stage;
27+
28+
import javafx.application.Platform;
29+
import org.junit.AfterClass;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class FocusedWindowMonocleTest extends FocusedWindowTestBase {
34+
35+
static {
36+
System.setProperty("glass.platform","Monocle");
37+
System.setProperty("monocle.platform","Headless");
38+
}
39+
40+
@BeforeClass
41+
public static void initFX() throws Exception {
42+
initFXBase();
43+
}
44+
45+
@Test
46+
public void testClosedFocusedStageLeak() throws Exception {
47+
testClosedFocusedStageLeakBase();
48+
}
49+
50+
@AfterClass
51+
public static void teardownOnce() {
52+
Platform.runLater(() -> {
53+
Platform.exit();
54+
});
55+
}
56+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package test.javafx.stage;
27+
28+
import javafx.application.Platform;
29+
import org.junit.AfterClass;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class FocusedWindowNativeTest extends FocusedWindowTestBase {
34+
35+
@BeforeClass
36+
public static void initFX() throws Exception {
37+
initFXBase();
38+
}
39+
40+
@Test
41+
public void testClosedFocusedStageLeak() throws Exception {
42+
testClosedFocusedStageLeakBase();
43+
}
44+
45+
@AfterClass
46+
public static void teardownOnce() {
47+
Platform.runLater(() -> {
48+
Platform.exit();
49+
});
50+
}
51+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package test.javafx.stage;
27+
28+
import javafx.application.Application;
29+
import javafx.application.Platform;
30+
import javafx.scene.Node;
31+
import javafx.scene.Scene;
32+
import javafx.scene.control.TextField;
33+
import javafx.stage.Stage;
34+
import javafx.stage.WindowEvent;
35+
36+
import java.lang.ref.WeakReference;
37+
import java.util.concurrent.CountDownLatch;
38+
import java.util.concurrent.TimeUnit;
39+
40+
import junit.framework.Assert;
41+
import test.util.Util;
42+
43+
public abstract class FocusedWindowTestBase {
44+
45+
static CountDownLatch startupLatch;
46+
static Stage stage = null;
47+
48+
public static void initFXBase() throws Exception {
49+
startupLatch = new CountDownLatch(1);
50+
Platform.startup(startupLatch::countDown);
51+
Platform.setImplicitExit(false);
52+
Assert.assertTrue("Timeout waiting for FX runtime to start",
53+
startupLatch.await(15, TimeUnit.MILLISECONDS));
54+
}
55+
56+
WeakReference<Stage> closedFocusedStageWeak = null;
57+
Stage closedFocusedStage = null;
58+
59+
public void testClosedFocusedStageLeakBase() throws Exception {
60+
CountDownLatch latch = new CountDownLatch(1);
61+
Util.runAndWait(() -> {
62+
closedFocusedStage = new Stage();
63+
closedFocusedStage.setTitle("Focused Stage");
64+
closedFocusedStageWeak = new WeakReference<>(closedFocusedStage);
65+
TextField textField = new TextField();
66+
closedFocusedStage.setScene(new Scene(textField));
67+
closedFocusedStage.setOnShown(l -> {
68+
latch.countDown();
69+
});
70+
closedFocusedStage.show();
71+
});
72+
Assert.assertTrue("Timeout waiting for closedFocusedStage to show`",
73+
latch.await(15, TimeUnit.MILLISECONDS));
74+
75+
CountDownLatch hideLatch = new CountDownLatch(1);
76+
closedFocusedStage.setOnHidden(a -> {
77+
hideLatch.countDown();
78+
});
79+
Util.runAndWait(() -> closedFocusedStage.close());
80+
Assert.assertTrue("Timeout waiting for closedFocusedStage to hide`",
81+
hideLatch.await(15, TimeUnit.MILLISECONDS));
82+
83+
closedFocusedStage.requestFocus();
84+
closedFocusedStage = null;
85+
assertCollectable(closedFocusedStageWeak);
86+
}
87+
88+
public static void assertCollectable(WeakReference weakReference) throws Exception {
89+
int counter = 0;
90+
91+
System.gc();
92+
System.runFinalization();
93+
94+
while (counter < 10 && weakReference.get() != null) {
95+
Thread.sleep(100);
96+
counter = counter + 1;
97+
System.gc();
98+
System.runFinalization();
99+
}
100+
101+
Assert.assertNull(weakReference.get());
102+
}
103+
}

0 commit comments

Comments
 (0)