Fix object leak from ThreadLocal.
This commit is contained in:
parent
e5b9b8eb81
commit
6dc6b4be92
|
@ -335,9 +335,11 @@ public final class Gson {
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<TypeToken<?>, FutureTypeAdapter<?>> threadCalls = calls.get();
|
Map<TypeToken<?>, FutureTypeAdapter<?>> threadCalls = calls.get();
|
||||||
|
boolean requiresThreadLocalCleanup = false;
|
||||||
if (threadCalls == null) {
|
if (threadCalls == null) {
|
||||||
threadCalls = new HashMap<TypeToken<?>, FutureTypeAdapter<?>>();
|
threadCalls = new HashMap<TypeToken<?>, FutureTypeAdapter<?>>();
|
||||||
calls.set(threadCalls);
|
calls.set(threadCalls);
|
||||||
|
requiresThreadLocalCleanup = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// the key and value type parameters always agree
|
// the key and value type parameters always agree
|
||||||
|
@ -346,9 +348,10 @@ public final class Gson {
|
||||||
return ongoingCall;
|
return ongoingCall;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
FutureTypeAdapter<T> call = new FutureTypeAdapter<T>();
|
FutureTypeAdapter<T> call = new FutureTypeAdapter<T>();
|
||||||
threadCalls.put(type, call);
|
threadCalls.put(type, call);
|
||||||
try {
|
|
||||||
for (TypeAdapterFactory factory : factories) {
|
for (TypeAdapterFactory factory : factories) {
|
||||||
TypeAdapter<T> candidate = factory.create(this, type);
|
TypeAdapter<T> candidate = factory.create(this, type);
|
||||||
if (candidate != null) {
|
if (candidate != null) {
|
||||||
|
@ -360,6 +363,10 @@ public final class Gson {
|
||||||
throw new IllegalArgumentException("GSON cannot handle " + type);
|
throw new IllegalArgumentException("GSON cannot handle " + type);
|
||||||
} finally {
|
} finally {
|
||||||
threadCalls.remove(type);
|
threadCalls.remove(type);
|
||||||
|
|
||||||
|
if (requiresThreadLocalCleanup) {
|
||||||
|
calls.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user