Skip to content
Permalink
Browse files
8264330: Scene MouseHandler is referencing removed nodes
Reviewed-by: kcr
  • Loading branch information
John Hendrikx authored and kevinrushforth committed Apr 1, 2021
1 parent eec2f39 commit 015dad0726fbd9cff731219b0cd24f727fc6c9e4
Showing with 52 additions and 2 deletions.
  1. +1 −0 build.gradle
  2. +2 −1 modules/javafx.graphics/src/main/java/javafx/scene/Scene.java
  3. +49 −1 modules/javafx.graphics/src/test/java/test/javafx/scene/SceneTest.java
@@ -2089,6 +2089,7 @@ project(":graphics") {
stubCompile group: "junit", name: "junit", version: "4.8.2"

antlr group: "org.antlr", name: "antlr4", version: "4.7.2", classifier: "complete"
testCompile project(":base").sourceSets.test.output
compile project(':base')
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, 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
@@ -3633,6 +3633,7 @@ private void clearPDREventTargets() {
currentEventTarget = currentEventTargets.size() > 0
? currentEventTargets.get(0) : null;
pdrEventTarget.clear();
pdrEventTargets.clear();
}

public void enterFullPDR(EventTarget gestureSource) {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, 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
@@ -30,6 +30,7 @@
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
@@ -53,6 +54,7 @@
import org.junit.Before;
import org.junit.Test;

import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
@@ -68,6 +70,7 @@
import javafx.scene.Scene;
import javafx.scene.SceneShim;
import javafx.scene.SubScene;
import test.util.memory.JMemoryBuddy;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
@@ -971,4 +974,49 @@ public void testAddNullPostLayoutPulseListener() {
SceneShim.scenePulseListener_pulse(scene);
assertEquals(1, counter.get());
}

@Test public void testNoReferencesRemainToRemovedNodeAfterBeingClicked() {
StubToolkit toolkit = (StubToolkit) Toolkit.getToolkit();
TilePane pane = new TilePane();
VBox vbox = new VBox(pane);
Scene scene = new Scene(vbox, 300, 200);
WeakReference<TilePane> ref = new WeakReference<>(pane);

pane.setMinSize(1000, 1000); // ensure mouse click will hit this node

stage.setScene(scene);

// Press mouse on TilePane so it gets picked up as a potential node to drag:
SceneHelper.processMouseEvent(
scene,
MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, 100, 100)
);

// Release mouse on TilePane to trigger clean up code as there will be no dragging:
SceneHelper.processMouseEvent(
scene,
MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, 100, 100)
);

// Remove TilePane, and replace with something else:
vbox.getChildren().setAll(new StackPane());

// Generate a MOUSE_EXITED event for the removed node and a pulse as otherwise many unrelated Scene references
// hang around to the removed node:
SceneHelper.processMouseEvent(
scene,
new MouseEvent(
MouseEvent.MOUSE_EXITED, 100, 100, 100, 100, MouseButton.NONE, 0, false, false, false,
false, false, false, false, false, false, true, null
)
);

toolkit.firePulse();

// Clear our own reference and see if the TilePane is now not referenced anywhere:
pane = null;

// Verify TilePane was GC'd:
JMemoryBuddy.assertCollectable(ref);
}
}

1 comment on commit 015dad0

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 015dad0 Apr 1, 2021

Please sign in to comment.