changeset 10812:9f7f82c485a4

8154039: Memory leak when selecting a tab which is not contained in TabPane::getTabs() Reviewed-by: kcr, aghaisas
author arapte
date Fri, 19 Jan 2018 11:18:31 +0530
parents adcac483ae85
children a455aff48e48
files modules/javafx.controls/src/main/java/javafx/scene/control/TabPane.java modules/javafx.controls/src/test/java/test/javafx/scene/control/TabPaneTest.java
diffstat 2 files changed, 36 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/TabPane.java	Fri Jan 19 10:21:30 2018 +0530
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/TabPane.java	Fri Jan 19 11:18:31 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, 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
@@ -752,9 +752,6 @@
                     return;
                 }
             }
-            if (tab != null) {
-                setSelectedItem(tab);
-            }
         }
 
         @Override protected Tab getModelItem(int index) {
--- a/modules/javafx.controls/src/test/java/test/javafx/scene/control/TabPaneTest.java	Fri Jan 19 10:21:30 2018 +0530
+++ b/modules/javafx.controls/src/test/java/test/javafx/scene/control/TabPaneTest.java	Fri Jan 19 11:18:31 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2018, 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
@@ -35,6 +35,7 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import test.com.sun.javafx.scene.control.infrastructure.StageLoader;
 import javafx.application.Platform;
@@ -83,6 +84,8 @@
 import javafx.scene.control.TabPane;
 import javafx.scene.control.TabPaneShim;
 
+import java.lang.ref.WeakReference;
+
 public class TabPaneTest {
     private TabPane tabPane;//Empty string
     private Toolkit tk;
@@ -1093,4 +1096,35 @@
         tabPane.getTabs().remove(t2);
     }
 
+    // Test for JDK-8154039
+    WeakReference<Tab> weakTab;
+    @Test public void testSelectNonChildTab() {
+        tabPane.getTabs().addAll(tab1);
+        root.getChildren().add(tabPane);
+        show();
+        tk.firePulse();
+        weakTab = new WeakReference<>(new Tab("NonChildTab"));
+        tabPane.getSelectionModel().select(weakTab.get());
+        tk.firePulse();
+        attemptGC(10);
+        tk.firePulse();
+        assertNull(weakTab.get());
+    }
+
+    private void attemptGC(int n) {
+        // Attempt gc n times
+        for (int i = 0; i < n; i++) {
+            System.gc();
+            System.runFinalization();
+
+            if (weakTab.get() == null) {
+                break;
+            }
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException e) {
+                fail("InterruptedException occurred during Thread.sleep()");
+            }
+        }
+    }
 }