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

PSwing nodes prevent proper garbage collection #8

Closed
GoogleCodeExporter opened this issue Mar 13, 2015 · 5 comments
Closed

PSwing nodes prevent proper garbage collection #8

GoogleCodeExporter opened this issue Mar 13, 2015 · 5 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1. add a PSwing node to another PNode
2. remove the PSwing node's parent from the canvas
3. force a garbage collection

What is the expected output? What do you see instead?
The PSwing node and its parent PNode should be collected because they are
no longer part of the canvas.  Instead, they are held in memory by the
Swing object that is contained in the PSwing node.

Here is the proposed fix to the PSwing class:
Index: C:/dev/piccolo.java-all/extras/edu/umd/cs/piccolox/pswing/PSwing.java
===================================================================
--- C:/dev/piccolo.java-all/extras/edu/umd/cs/piccolox/pswing/PSwing.java
(revision 3)
+++ C:/dev/piccolo.java-all/extras/edu/umd/cs/piccolox/pswing/PSwing.java
(revision 4)
@@ -187,10 +187,12 @@
     /*The parent listener for camera/canvas changes*/
     private PropertyChangeListener parentListener = new
PropertyChangeListener() {
         public void propertyChange( PropertyChangeEvent evt ) {
-            PNode source = (PNode)evt.getSource();
-            PNode parent = source.getParent();
+            PNode parent = (PNode) evt.getNewValue();
+            clearListeners((PNode) evt.getOldValue());
             if( parent != null ) {
                 listenForCanvas( parent );
+            } else {
+                updateCanvas( null );
             }

         }
@@ -478,7 +480,12 @@

             PNode parent = p;
 //            System.out.println( "parent = " + parent.getClass() );
-            if( parent instanceof PLayer ) {
+            if (parent instanceof PCamera) {
+                PCamera cam = (PCamera) parent;
+                if( cam.getComponent() instanceof PSwingCanvas ) {
+                    updateCanvas( (PSwingCanvas)cam.getComponent() );
+                }
+            } else if( parent instanceof PLayer ) {
                 PLayer player = (PLayer)parent;
 //                System.out.println( "Found player: with " +
player.getCameraCount() + " cameras" );
                 for( int i = 0; i < player.getCameraCount(); i++ ) {
@@ -521,6 +528,22 @@
     }

     /**
+     * Clear out all the listeners registered to make sure there are no
stray references
+     *
+     * @param fromParent Parent to start with for clearing listeners
+     */
+    private void clearListeners(PNode fromParent) {
+        if (fromParent == null) {
+            return;
+        }
+        if (listeningTo(fromParent)) {
+            fromParent.removePropertyChangeListener(
PNode.PROPERTY_PARENT, parentListener );
+            listeningTo.remove(fromParent);
+            clearListeners(fromParent.getParent());
+        }
+    }
+
+    /**
      * Removes this PSwing from previous PSwingCanvas (if any), and ensure
that this PSwing is attached to the new PSwingCanvas.
      * @param newCanvas the new PSwingCanvas (may be null)
      */
@@ -529,8 +552,8 @@
             if( canvas != null ) {
                 canvas.removePSwing( this );
             }
+            canvas = newCanvas;
             if( newCanvas != null ) {
-                canvas = newCanvas;
                 canvas.addPSwing( this );
                 reshape();
                 repaint();

Original issue reported on code.google.com by steveonjava on 23 Jun 2008 at 11:18

@GoogleCodeExporter
Copy link
Author

Original comment by steveonjava on 25 Jun 2008 at 6:35

  • Added labels: Milestone-1.3

@GoogleCodeExporter
Copy link
Author

Original comment by steveonjava on 2 Jul 2008 at 12:28

  • Added labels: Milestone-2.0

@GoogleCodeExporter
Copy link
Author

Original comment by steveonjava on 3 Jul 2008 at 5:05

  • Added labels: Milestone-1.3

@GoogleCodeExporter
Copy link
Author

Fixed by change 410 including a test case to reproduce this.

Original comment by steveonjava on 9 Nov 2008 at 10:51

  • Changed state: Fixed

@GoogleCodeExporter
Copy link
Author

Original comment by heue...@gmail.com on 28 Jul 2009 at 1:51

  • Changed state: Verified

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant