Hi,

OK, slightly more elegant fix for the JTree leak.  I'm not sure if this
covers *all* possible cases where we'd need to remove an entry out of
nodeStates, but it seems to cover the ones that I was testing for.

How's this look?

Thanks,
Francis

On Thu, 2006-11-02 at 22:29 +0100, Roman Kennke wrote:
> Hi Francis,
> 
> 
> > The attached patch fixes a memory leak in the JTree, changing a
> > Hashtable into a WeakHashMap.
> > 
> > I'm not too familiar with this class, so I don't know if this is the
> > best way to fix it (or if this even breaks something!), and I'd
> > appreciate any comments or approval for this patch.
> 
> This would probably not break things. But using a WeakHashMap doesn't
> seem like the best solution here. We should rather make sure that no
> entries are left in the table. I'm thinking that in the TreeModelHandler
> inner class we can take care of that cleanly without the need to use
> WeakHashMap.
> 
> /Roman
> 
> 
Index: javax/swing/JTree.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTree.java,v
retrieving revision 1.77
diff -u -r1.77 JTree.java
--- javax/swing/JTree.java	18 Oct 2006 18:46:25 -0000	1.77
+++ javax/swing/JTree.java	6 Nov 2006 13:54:29 -0000
@@ -2201,20 +2201,30 @@
 
   public void setSelectionPath(TreePath path)
   {
+    clearSelectionPathStates();
     selectionModel.setSelectionPath(path);
   }
 
   public void setSelectionPaths(TreePath[] paths)
   {
+    clearSelectionPathStates();
     selectionModel.setSelectionPaths(paths);
   }
+  
+  private void clearSelectionPathStates()
+  {
+    TreePath[] oldPaths = selectionModel.getSelectionPaths();
+    if (oldPaths != null)
+      for (int i = 0; i < oldPaths.length; i++)
+        nodeStates.remove(oldPaths[i]);
+  }
 
   public void setSelectionRow(int row)
   {
     TreePath path = getPathForRow(row);
 
     if (path != null)
-      selectionModel.setSelectionPath(path);
+      setSelectionPath(path);
   }
 
   public void setSelectionRows(int[] rows)
@@ -2289,11 +2299,13 @@
 
   public void removeSelectionPath(TreePath path)
   {
+    clearSelectionPathStates();
     selectionModel.removeSelectionPath(path);
   }
 
   public void removeSelectionPaths(TreePath[] paths)
   {
+    clearSelectionPathStates();
     selectionModel.removeSelectionPaths(paths);
   }
 
@@ -2302,7 +2314,7 @@
     TreePath path = getPathForRow(row);
 
     if (path != null)
-      selectionModel.removeSelectionPath(path);
+      removeSelectionPath(path);
   }
 
   public void removeSelectionRows(int[] rows)

Reply via email to