Reviewers: rjrjr, bobv,

Description:
Issue 6193: Fix memory-leak in WeakMapping when the value holds a
reference on the key

http://code.google.com/p/google-web-toolkit/issues/detail?id=6193

Fix memory-leak in WeakMapping when the value holds a reference on the
key, which prevents the entry from being garbage collected.

We've run with this fix for a whole week and had no (reported) issue.

Please review this at http://gwt-code-reviews.appspot.com/1401802/

Affected files:
  M user/src/com/google/gwt/core/client/impl/WeakMapping.java


Index: user/src/com/google/gwt/core/client/impl/WeakMapping.java
diff --git a/user/src/com/google/gwt/core/client/impl/WeakMapping.java b/user/src/com/google/gwt/core/client/impl/WeakMapping.java index 435816c0a15f8b879a432f909770a187babf3fda..37f21d752fade7f456cbdc15bb97afdb49b75644 100644
--- a/user/src/com/google/gwt/core/client/impl/WeakMapping.java
+++ b/user/src/com/google/gwt/core/client/impl/WeakMapping.java
@@ -93,7 +93,8 @@ public class WeakMapping {
* identity. Weak references are used to allow otherwise unreferenced Objects
    * to be garbage collected.
    */
- private static Map<IdentityWeakReference, Map<String, Object>> map = new HashMap<IdentityWeakReference, Map<String, Object>>(); + private static Map<IdentityWeakReference, Map<String, WeakReference<Object>>> map = + new HashMap<IdentityWeakReference, Map<String, WeakReference<Object>>>();

   /**
    * A ReferenceQueue used to clean up the map as its keys are
@@ -113,7 +114,7 @@ public class WeakMapping {
     cleanup();

     Object ref = new IdentityWeakReference(instance, queue);
-    Map<String, Object> m = map.get(ref);
+    Map<String, WeakReference<Object>> m = map.get(ref);
     if (m == null) {
       return null;
     }
@@ -143,12 +144,12 @@ public class WeakMapping {
     }

     IdentityWeakReference ref = new IdentityWeakReference(instance, queue);
-    Map<String, Object> m = map.get(ref);
+    Map<String, WeakReference<Object>> m = map.get(ref);
     if (m == null) {
-      m = new HashMap<String, Object>();
+      m = new HashMap<String, WeakReference<Object>>();
       map.put(ref, m);
     }
-    m.put(key, value);
+    m.put(key, new WeakReference<Object>(value));
   }

   /**


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to