Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 15a35d6

Browse files
committed
prevent memory leak
1 parent b3901ad commit 15a35d6

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

patchwork-loot/src/main/java/net/patchworkmc/mixin/loot/MixinLootManager.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
import java.io.IOException;
2323
import java.util.Deque;
2424
import java.util.Map;
25+
import java.util.function.BiConsumer;
2526

2627
import com.google.common.collect.ImmutableMap;
2728
import com.google.common.collect.Queues;
2829
import com.google.gson.Gson;
2930
import com.google.gson.JsonElement;
3031
import com.google.gson.JsonObject;
3132
import com.google.gson.JsonParseException;
33+
import jdk.nashorn.internal.codegen.CompilerConstants;
3234
import org.spongepowered.asm.mixin.Mixin;
3335
import org.spongepowered.asm.mixin.Unique;
3436
import org.spongepowered.asm.mixin.injection.At;
@@ -50,12 +52,12 @@ public abstract class MixinLootManager extends MixinJsonDataLoader {
5052
@Unique
5153
private ResourceManager resourceManager;
5254

55+
// TODO: is reentrancy necessary?
5356
@Unique
5457
private static ThreadLocal<Deque<LootManager>> lootContext = new ThreadLocal<Deque<LootManager>>();
5558

56-
// TODO: is reentrancy necessary?
57-
@Inject(method = "apply", at = @At("HEAD"))
58-
private void getResourceManager(Map<Identifier, JsonObject> map, ResourceManager resourceManager, Profiler profiler, CallbackInfo info) {
59+
@Redirect(method = "apply", at = @At(value = "INVOKE", target = "java/util/Map.forEach (Ljava/util/function/BiConsumer;)V"))
60+
private void handleContext(Map<Identifier, JsonObject> map, BiConsumer<Identifier, JsonObject> consumer, Map<Identifier, JsonObject> sameMap, ResourceManager resourceManager, Profiler profiler) {
5961
this.resourceManager = resourceManager;
6062
Deque<LootManager> que = lootContext.get();
6163

@@ -65,13 +67,13 @@ private void getResourceManager(Map<Identifier, JsonObject> map, ResourceManager
6567
}
6668

6769
que.push((LootManager) (Object) this);
68-
}
6970

70-
@Inject(method = "apply", at = @At("RETURN"))
71-
private void delResourceManager(CallbackInfo info) {
72-
// TODO: what if an exception is thrown?
73-
resourceManager = null;
74-
lootContext.get().pop();
71+
try {
72+
map.forEach(consumer);
73+
} finally {
74+
this.resourceManager = null;
75+
lootContext.get().pop();
76+
}
7577
}
7678

7779
@Redirect(method = "method_20711", at = @At(value = "INVOKE", target = "com/google/gson/Gson.fromJson (Lcom/google/gson/JsonElement;Ljava/lang/Class;)Ljava/lang/Object;"))

0 commit comments

Comments
 (0)