Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8241840: Memoryleak: Closed focused Stages are not collected with Monocle. #153

Closed
wants to merge 11 commits into from
@@ -121,7 +121,7 @@ boolean maximizeWindow(MonocleWindow window) {

boolean requestFocus(MonocleWindow window) {
int index = getWindowIndex(window);
if (index != -1) {
if (index != -1 && window.isVisible()) {
This conversation was marked as resolved by FlorianKirmaier

This comment has been minimized.

@arapte

arapte Apr 17, 2020
Member

This change is sufficient for the test to pass, are there any other scenarios for which the changes in Window.java are needed. If so could you please add the relevant tests.

This comment has been minimized.

@FlorianKirmaier

FlorianKirmaier Apr 22, 2020
Author Member

You are correct. The change happened, because of the leak for first noticeable in the Window class. I've now removed the change from the Window class because it's not required.

focusedWindow = window;
window.notifyFocus(WindowEvent.FOCUS_GAINED);
return true;
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.javafx.stage;

import javafx.application.Platform;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class FocusedWindowMonocleTest extends FocusedWindowTestBase {

static {
System.setProperty("glass.platform","Monocle");
System.setProperty("monocle.platform","Headless");
}

@BeforeClass
public static void initFX() throws Exception {
initFXBase();
}

@Test
public void testClosedFocusedStageLeak() throws Exception {
testClosedFocusedStageLeakBase();
}

@AfterClass
public static void teardownOnce() {
Platform.runLater(() -> {
Platform.exit();
});
}
}
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.javafx.stage;

import javafx.application.Platform;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class FocusedWindowNativeTest extends FocusedWindowTestBase {

@BeforeClass
public static void initFX() throws Exception {
initFXBase();
}

@Test
public void testClosedFocusedStageLeak() throws Exception {
testClosedFocusedStageLeakBase();
}

@AfterClass
public static void teardownOnce() {
Platform.runLater(() -> {
Platform.exit();
});
}
}
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.javafx.stage;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;
import test.util.Util;

public abstract class FocusedWindowTestBase {

static CountDownLatch startupLatch;
static Stage stage = null;

public static void initFXBase() throws Exception {
startupLatch = new CountDownLatch(1);
Platform.startup(startupLatch::countDown);
Platform.setImplicitExit(false);
Assert.assertTrue("Timeout waiting for FX runtime to start",
startupLatch.await(15, TimeUnit.MILLISECONDS));
}

WeakReference<Stage> closedFocusedStageWeak = null;
Stage closedFocusedStage = null;

public void testClosedFocusedStageLeakBase() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Util.runAndWait(() -> {
closedFocusedStage = new Stage();
closedFocusedStage.setTitle("Focused Stage");
closedFocusedStageWeak = new WeakReference<>(closedFocusedStage);
TextField textField = new TextField();
closedFocusedStage.setScene(new Scene(textField));
closedFocusedStage.setOnShown(l -> {
latch.countDown();
});
closedFocusedStage.show();
});
Assert.assertTrue("Timeout waiting for closedFocusedStage to show`",
latch.await(15, TimeUnit.MILLISECONDS));

CountDownLatch hideLatch = new CountDownLatch(1);
closedFocusedStage.setOnHidden(a -> {
hideLatch.countDown();
});
Util.runAndWait(() -> closedFocusedStage.close());
Assert.assertTrue("Timeout waiting for closedFocusedStage to hide`",
hideLatch.await(15, TimeUnit.MILLISECONDS));

closedFocusedStage.requestFocus();
closedFocusedStage = null;
assertCollectable(closedFocusedStageWeak);
}

public static void assertCollectable(WeakReference weakReference) throws Exception {
int counter = 0;

System.gc();
System.runFinalization();

while (counter < 10 && weakReference.get() != null) {
Thread.sleep(100);
counter = counter + 1;
System.gc();
System.runFinalization();
}

Assert.assertNull(weakReference.get());
}
}
ProTip! Use n and p to navigate between commits in a pull request.