Hi,
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.
Thanks,
Francis
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 2 Nov 2006 21:02:28 -0000
@@ -52,6 +52,7 @@
import java.util.Iterator;
import java.util.Locale;
import java.util.Vector;
+import java.util.WeakHashMap;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleAction;
@@ -1400,7 +1401,7 @@
* TreePath of a note to to its state. Valid states are EXPANDED and
* COLLAPSED. Nodes not in this Hashtable are assumed state COLLAPSED.
*/
- private Hashtable nodeStates = new Hashtable();
+ private WeakHashMap nodeStates = new WeakHashMap();
protected transient TreeCellEditor cellEditor;
@@ -1505,7 +1506,7 @@
setSelectionModel( new DefaultTreeSelectionModel() );
// The root node appears expanded by default.
- nodeStates = new Hashtable();
+ nodeStates = new WeakHashMap();
// The cell renderer gets set by the UI.
cellRenderer = null;
@@ -2757,12 +2758,12 @@
if (parent == null)
return null;
- Enumeration nodes = nodeStates.keys();
+ Iterator nodes = nodeStates.keySet().iterator();
Vector result = new Vector();
- while (nodes.hasMoreElements())
+ while (nodes.hasNext())
{
- TreePath path = (TreePath) nodes.nextElement();
+ TreePath path = (TreePath) nodes.next();
if (path.isDescendant(parent))
result.addElement(path);
@@ -2903,11 +2904,11 @@
*/
public Enumeration getExpandedDescendants(TreePath path)
{
- Enumeration paths = nodeStates.keys();
+ Iterator paths = nodeStates.keySet().iterator();
Vector relevantPaths = new Vector();
- while (paths.hasMoreElements())
+ while (paths.hasNext())
{
- TreePath nextPath = (TreePath) paths.nextElement();
+ TreePath nextPath = (TreePath) paths.next();
if (nodeStates.get(nextPath) == EXPANDED
&& path.isDescendant(nextPath))
{