mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Fix event node leak
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
caa150d923
commit
b4af87b8a4
@ -18,13 +18,7 @@ import java.util.function.Consumer;
|
||||
non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
||||
static final Object GLOBAL_CHILD_LOCK = new Object();
|
||||
|
||||
private final ClassValue<Handle<T>> handleMap = new ClassValue<>() {
|
||||
@Override
|
||||
protected Handle<T> computeValue(Class<?> type) {
|
||||
//noinspection unchecked
|
||||
return new Handle<>((Class<T>) type);
|
||||
}
|
||||
};
|
||||
private final Map<Class, Handle<T>> handleMap = new ConcurrentHashMap<>();
|
||||
final Map<Class<? extends T>, ListenerEntry<T>> listenerMap = new ConcurrentHashMap<>();
|
||||
final Set<EventNodeImpl<T>> children = new CopyOnWriteArraySet<>();
|
||||
final Map<Object, EventNodeImpl<T>> mappedNodeCache = new WeakHashMap<>();
|
||||
@ -49,7 +43,8 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <E extends T> @NotNull ListenerHandle<E> getHandle(@NotNull Class<E> handleType) {
|
||||
return (ListenerHandle<E>) handleMap.get(handleType);
|
||||
return (ListenerHandle<E>) handleMap.computeIfAbsent(handleType,
|
||||
aClass -> new Handle<>((Class<T>) aClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -272,7 +267,8 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
||||
|
||||
private void invalidateEvent(Class<? extends T> eventClass) {
|
||||
forTargetEvents(eventClass, type -> {
|
||||
Handle<T> handle = handleMap.get(type);
|
||||
Handle<T> handle = handleMap.computeIfAbsent(type,
|
||||
aClass -> new Handle<>((Class<T>) aClass));
|
||||
handle.invalidate();
|
||||
});
|
||||
final EventNodeImpl<? super T> parent = this.parent;
|
||||
|
@ -10,9 +10,11 @@ import net.minestom.server.item.Material;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static net.minestom.testing.TestUtils.waitUntilCleared;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class EventNodeTest {
|
||||
@ -220,4 +222,22 @@ public class EventNodeTest {
|
||||
node.call(new ItemTestEvent(ItemStack.of(Material.DIAMOND)));
|
||||
assertFalse(result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nodeEmptyGC() {
|
||||
var node = EventNode.all("main");
|
||||
var ref = new WeakReference<>(node);
|
||||
node = null;
|
||||
waitUntilCleared(ref);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nodeGC() {
|
||||
var node = EventNode.all("main");
|
||||
var ref = new WeakReference<>(node);
|
||||
node.addListener(EventTest.class, event -> {
|
||||
});
|
||||
node = null;
|
||||
waitUntilCleared(ref);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user