From 2b5f6ccc1e9ae5c9c8c88dc3195020eaa27a3be6 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 26 Mar 2013 01:41:48 +0200 Subject: Backported fix of minimal (empty hashmap) leak on redeploy (#9993) Change-Id: I9933c21ab8449378269c577c9622d75586590e45 Merge: no --- server/src/com/vaadin/util/CurrentInstance.java | 35 ++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'server/src') diff --git a/server/src/com/vaadin/util/CurrentInstance.java b/server/src/com/vaadin/util/CurrentInstance.java index adf6d963c3..595c162e7e 100644 --- a/server/src/com/vaadin/util/CurrentInstance.java +++ b/server/src/com/vaadin/util/CurrentInstance.java @@ -36,6 +36,10 @@ public class CurrentInstance implements Serializable { @Override protected Map, CurrentInstance> childValue( Map, CurrentInstance> parentValue) { + if (parentValue == null) { + return null; + } + Map, CurrentInstance> value = new HashMap, CurrentInstance>(); // Copy all inheritable values to child map @@ -47,11 +51,6 @@ public class CurrentInstance implements Serializable { return value; } - - @Override - protected Map, CurrentInstance> initialValue() { - return new HashMap, CurrentInstance>(); - } }; private CurrentInstance(Object instance, boolean inheritable) { @@ -68,7 +67,11 @@ public class CurrentInstance implements Serializable { * if there is no current instance. */ public static T get(Class type) { - CurrentInstance currentInstance = instances.get().get(type); + Map, CurrentInstance> map = instances.get(); + if (map == null) { + return null; + } + CurrentInstance currentInstance = map.get(type); if (currentInstance != null) { return type.cast(currentInstance.instance); } else { @@ -110,11 +113,25 @@ public class CurrentInstance implements Serializable { } private static void set(Class type, T instance, boolean inheritable) { + Map, CurrentInstance> map = instances.get(); if (instance == null) { - instances.get().remove(type); + // remove the instance + if (map == null) { + return; + } + map.remove(type); + if (map.isEmpty()) { + instances.remove(); + map = null; + } } else { assert type.isInstance(instance) : "Invald instance type"; - CurrentInstance previousInstance = instances.get().put(type, + if (map == null) { + map = new HashMap, CurrentInstance>(); + instances.set(map); + } + + CurrentInstance previousInstance = map.put(type, new CurrentInstance(instance, inheritable)); if (previousInstance != null) { assert previousInstance.inheritable == inheritable : "Inheritable status mismatch for " @@ -127,6 +144,6 @@ public class CurrentInstance implements Serializable { * Clears all current instances. */ public static void clearAll() { - instances.get().clear(); + instances.remove(); } } -- cgit v1.2.3