From: Andreas L. Delmelle Date: Fri, 20 Jul 2007 16:20:23 +0000 (+0000) Subject: Fix possible memory leak in PropertyCache X-Git-Tag: fop-0_95beta~431 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=afa000d0c1addce587f592e0b246054ec0f9b156;p=xmlgraphics-fop.git Fix possible memory leak in PropertyCache git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@558041 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/fo/properties/PropertyCache.java b/src/java/org/apache/fop/fo/properties/PropertyCache.java index 0685ff514..9acf12513 100644 --- a/src/java/org/apache/fop/fo/properties/PropertyCache.java +++ b/src/java/org/apache/fop/fo/properties/PropertyCache.java @@ -19,6 +19,7 @@ package org.apache.fop.fo.properties; +import java.lang.ref.WeakReference; import java.util.Collections; import java.util.Map; import java.util.WeakHashMap; @@ -35,6 +36,10 @@ public class PropertyCache { private Map propCache = Collections.synchronizedMap(new WeakHashMap()); + public int size() { + return propCache.size(); + } + /** * Checks if the given property is present in the cache - if so, returns * a reference to the cached value. Otherwise the given object is added @@ -44,12 +49,14 @@ public class PropertyCache { */ public Property fetch(Property prop) { - Property cacheEntry = (Property) propCache.get(prop); - if (cacheEntry != null) { - return cacheEntry; - } else { - propCache.put(prop, prop); - return prop; + WeakReference ref = (WeakReference) propCache.get(prop); + if (ref != null) { + Property cacheEntry = (Property)ref.get(); + if (cacheEntry != null) { + return cacheEntry; + } } + propCache.put(prop, new WeakReference(prop)); + return prop; } }