changeset 1256:e1200e6556ab

RT-20285 memory leak in javafx.scene.controls.MenuBar
author Paru Somashekar <paru.somashekar@oracle.com>
date Fri, 08 Jun 2012 11:23:50 -0700
parents c7813fd4d4a0
children 527e107cfec5
files javafx-ui-controls/src/com/sun/javafx/scene/control/skin/MenuBarSkin.java
diffstat 1 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/MenuBarSkin.java	Fri Jun 08 18:15:33 2012 +0100
+++ b/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/MenuBarSkin.java	Fri Jun 08 11:23:50 2012 -0700
@@ -65,6 +65,9 @@
 import com.sun.javafx.scene.traversal.TraverseListener;
 import com.sun.javafx.stage.StageHelper;
 import com.sun.javafx.tk.Toolkit;
+import java.lang.ref.WeakReference;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import javafx.event.ActionEvent;
 import javafx.event.EventType;
 import javafx.scene.input.*;
@@ -149,7 +152,7 @@
     private WeakEventHandler weakSceneMouseEventHandler;
     private EventHandler keyEventHandler;
     private EventHandler mouseEventHandler;
-
+    
     /***************************************************************************
      *                                                                         *
      * Constructors                                                            *
@@ -234,6 +237,7 @@
         };
         weakSceneMouseEventHandler = new WeakEventHandler(control.getScene(), MouseEvent.MOUSE_CLICKED, 
                 mouseEventHandler);
+        control.getScene().addEventFilter(MouseEvent.MOUSE_CLICKED, weakSceneMouseEventHandler);
         
         // When the parent window looses focus - menu selection should be cleared
         control.getScene().getWindow().focusedProperty().addListener(new WeakChangeListener(new ChangeListener<Boolean>() {
@@ -300,9 +304,23 @@
         };
         engine.addTraverseListener(this);
         setImpl_traversalEngine(engine);
+        
+        control.sceneProperty().addListener(new ChangeListener<Scene>() {
+            @Override
+            public void changed(ObservableValue<? extends Scene> ov, Scene t, Scene t1) {
+                if (weakSceneKeyEventHandler != null) {
+                    // remove event filter from the old scene (t)
+                    t.removeEventFilter(KeyEvent.KEY_PRESSED, weakSceneKeyEventHandler);
+                }
+                if (weakSceneMouseEventHandler != null) {
+                    // remove event filter from the old scene (t)
+                    t.removeEventFilter(MouseEvent.MOUSE_CLICKED, weakSceneMouseEventHandler);
+                }
+            }
+        });
     }
     
-
+    
     Runnable firstMenuRunnable = new Runnable() {
             public void run() {
                 /*