ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maart...@apache.org
Subject svn commit: r1374865 - /ant/ivy/core/trunk/src/java/org/apache/ivy/core/module/id/ModuleId.java
Date Sun, 19 Aug 2012 21:55:05 GMT
Author: maartenc
Date: Sun Aug 19 21:55:05 2012
New Revision: 1374865

URL: http://svn.apache.org/viewvc?rev=1374865&view=rev
Log:
FIX: Memory leak and infinite loop in ModuleId.java (IVY-1362) (2nd attempt)

Modified:
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/module/id/ModuleId.java

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/module/id/ModuleId.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/module/id/ModuleId.java?rev=1374865&r1=1374864&r2=1374865&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/module/id/ModuleId.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/module/id/ModuleId.java Sun Aug 19 21:55:05
2012
@@ -17,6 +17,7 @@
  */
 package org.apache.ivy.core.module.id;
 
+import java.lang.ref.WeakReference;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.WeakHashMap;
@@ -33,7 +34,7 @@ import org.apache.ivy.core.IvyPatternHel
 public class ModuleId implements Comparable {
     static final String ENCODE_SEPARATOR = ":#@#:";
     
-    private static final Map/*<ModuleId, ModuleId>*/ CACHE = new WeakHashMap();
+    private static final Map/*<ModuleId, WeakReference<ModuleId>>*/ CACHE = new
WeakHashMap();
 
     /**
      * Returns a ModuleId for the given organization and module name.
@@ -59,12 +60,20 @@ public class ModuleId implements Compara
      *            the module id to return
      * @return a unit instance of the given module id.
      */
-    public static synchronized ModuleId intern(ModuleId moduleId) {
-        ModuleId r = (ModuleId) CACHE.get(moduleId);
-        if (r == null) {
-            r = moduleId;
-            CACHE.put(r, r);
+    public static ModuleId intern(ModuleId moduleId) {
+        ModuleId r = null;
+        
+        synchronized (CACHE) {
+            WeakReference ref = (WeakReference) CACHE.get(moduleId);
+            if (ref != null) {
+                r = (ModuleId) ref.get();
+            }
+            if (r == null) {
+                r = moduleId;
+                CACHE.put(r, new WeakReference(r));
+            }
         }
+
         return r;
     }
 



Mime
View raw message