mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +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> {
|
non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
||||||
static final Object GLOBAL_CHILD_LOCK = new Object();
|
static final Object GLOBAL_CHILD_LOCK = new Object();
|
||||||
|
|
||||||
private final ClassValue<Handle<T>> handleMap = new ClassValue<>() {
|
private final Map<Class, Handle<T>> handleMap = new ConcurrentHashMap<>();
|
||||||
@Override
|
|
||||||
protected Handle<T> computeValue(Class<?> type) {
|
|
||||||
//noinspection unchecked
|
|
||||||
return new Handle<>((Class<T>) type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
final Map<Class<? extends T>, ListenerEntry<T>> listenerMap = new ConcurrentHashMap<>();
|
final Map<Class<? extends T>, ListenerEntry<T>> listenerMap = new ConcurrentHashMap<>();
|
||||||
final Set<EventNodeImpl<T>> children = new CopyOnWriteArraySet<>();
|
final Set<EventNodeImpl<T>> children = new CopyOnWriteArraySet<>();
|
||||||
final Map<Object, EventNodeImpl<T>> mappedNodeCache = new WeakHashMap<>();
|
final Map<Object, EventNodeImpl<T>> mappedNodeCache = new WeakHashMap<>();
|
||||||
@ -49,7 +43,8 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <E extends T> @NotNull ListenerHandle<E> getHandle(@NotNull Class<E> handleType) {
|
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
|
@Override
|
||||||
@ -272,7 +267,8 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
|
|
||||||
private void invalidateEvent(Class<? extends T> eventClass) {
|
private void invalidateEvent(Class<? extends T> eventClass) {
|
||||||
forTargetEvents(eventClass, type -> {
|
forTargetEvents(eventClass, type -> {
|
||||||
Handle<T> handle = handleMap.get(type);
|
Handle<T> handle = handleMap.computeIfAbsent(type,
|
||||||
|
aClass -> new Handle<>((Class<T>) aClass));
|
||||||
handle.invalidate();
|
handle.invalidate();
|
||||||
});
|
});
|
||||||
final EventNodeImpl<? super T> parent = this.parent;
|
final EventNodeImpl<? super T> parent = this.parent;
|
||||||
|
@ -10,9 +10,11 @@ import net.minestom.server.item.Material;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import static net.minestom.testing.TestUtils.waitUntilCleared;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class EventNodeTest {
|
public class EventNodeTest {
|
||||||
@ -220,4 +222,22 @@ public class EventNodeTest {
|
|||||||
node.call(new ItemTestEvent(ItemStack.of(Material.DIAMOND)));
|
node.call(new ItemTestEvent(ItemStack.of(Material.DIAMOND)));
|
||||||
assertFalse(result.get());
|
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