modules/graphics/src/main/java/javafx/scene/Parent.java

Print this page

        

@@ -495,11 +495,14 @@
                     }
                     if (old.getParent() == Parent.this) {
                         old.setParent(null);
                         old.setScenes(null, null);
                     }
-                    if (!removedChildrenOptimizationDisabled) {
+                    // Do not add parent with null scene to the removed list.
+                    // It will not be processed in the list and its memory
+                    // will not be freed.
+                    if (scene != null && !removedChildrenOptimizationDisabled) {
                         removed.add(old);
                     }
                 }
             }
         }

@@ -644,17 +647,25 @@
                 childrenTriggerPermutation = false;
             }
         }
     }
 
+    // This method is to do cleanup on parent when it is removed from a scene 
+    private void nullSceneCleanup() {
+        if (removed != null) {
+            removed.clear();
+        }
+    }
+
     @Override
     void scenesChanged(final Scene newScene, final SubScene newSubScene,
                        final Scene oldScene, final SubScene oldSubScene) {
 
         if (oldScene != null && newScene == null) {
             // RT-34863 - clean up CSS cache when Parent is removed from scene-graph
             StyleManager.getInstance().forget(this);
+            nullSceneCleanup();
         }
 
         for (int i=0; i<children.size(); i++) {
             children.get(i).setScenes(newScene, newSubScene);
         }