From 6dc6b4be9208221caad81b52aec665f133b5e9af Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Mon, 14 Jan 2013 22:05:28 +0000 Subject: [PATCH] Fix object leak from ThreadLocal. --- gson/src/main/java/com/google/gson/Gson.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 9dbbd3a7..ab63fdc0 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -335,9 +335,11 @@ public final class Gson { } Map, FutureTypeAdapter> threadCalls = calls.get(); + boolean requiresThreadLocalCleanup = false; if (threadCalls == null) { threadCalls = new HashMap, FutureTypeAdapter>(); calls.set(threadCalls); + requiresThreadLocalCleanup = true; } // the key and value type parameters always agree @@ -346,9 +348,10 @@ public final class Gson { return ongoingCall; } - FutureTypeAdapter call = new FutureTypeAdapter(); - threadCalls.put(type, call); try { + FutureTypeAdapter call = new FutureTypeAdapter(); + threadCalls.put(type, call); + for (TypeAdapterFactory factory : factories) { TypeAdapter candidate = factory.create(this, type); if (candidate != null) { @@ -360,6 +363,10 @@ public final class Gson { throw new IllegalArgumentException("GSON cannot handle " + type); } finally { threadCalls.remove(type); + + if (requiresThreadLocalCleanup) { + calls.remove(); + } } }