From 0c718f94841baaea13d511f6742142e1249ecd45 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 13 Jan 2014 03:46:06 +0100 Subject: [PATCH] Attempting to correct a memory leak in ticket #189. --- .../comphenix/protocol/injector/PacketFilterManager.java | 3 ++- .../protocol/injector/netty/InjectionFactory.java | 8 ++++---- .../protocol/injector/netty/NettyProtocolInjector.java | 4 ++-- .../protocol/injector/player/PlayerInjectionHandler.java | 6 +++--- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java index e7c356b5..dc5215af 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -921,8 +921,9 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok * @param players - list of players to uninject. */ public void uninitializePlayers(Player[] players) { - for (Player player : players) + for (Player player : players) { playerInjection.uninjectPlayer(player); + } } /** diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java index 10f81982..7f5da749 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java @@ -14,7 +14,6 @@ import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.utility.MinecraftFields; import com.comphenix.protocol.utility.MinecraftReflection; import com.google.common.collect.MapMaker; -import com.google.common.collect.Maps; /** * Represents an injector factory. @@ -23,11 +22,12 @@ import com.google.common.collect.Maps; * @author Kristian */ class InjectionFactory { - private final ConcurrentMap playerLookup = Maps.newConcurrentMap(); + // This should work as long as the injectors are, uh, injected + private final ConcurrentMap playerLookup = new MapMaker().weakKeys().weakValues().makeMap(); private final ConcurrentMap nameLookup = new MapMaker().weakValues().makeMap(); // Whether or not the factory is closed - private boolean closed; + private volatile boolean closed; /** * Construct or retrieve a channel injector from an existing Bukkit player. @@ -44,7 +44,7 @@ class InjectionFactory { // Find a temporary injector as well if (injector == null) injector = getTemporaryInjector(player); - if (injector != null) + if (injector != null && !injector.isClosed()) return injector; Object networkManager = MinecraftFields.getNetworkManager(player); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java index 4a4b6314..c10aa545 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java @@ -328,10 +328,10 @@ public class NettyProtocolInjector implements ChannelListener { @Override public boolean uninjectPlayer(Player player) { - injectionFactory.fromPlayer(player, listener).close(); + // Just let Netty clean this up return true; } - + @Override public void sendServerPacket(Player receiver, PacketContainer packet, NetworkMarker marker, boolean filters) throws InvocationTargetException { injectionFactory.fromPlayer(receiver, listener). diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java index b35015c0..470db859 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjectionHandler.java @@ -98,12 +98,12 @@ public interface PlayerInjectionHandler { public abstract void handleDisconnect(Player player); /** - * Unregisters the given player. - * @param player - player to unregister. + * Uninject the given player. + * @param player - player to uninject. * @return TRUE if a player has been uninjected, FALSE otherwise. */ public abstract boolean uninjectPlayer(Player player); - + /** * Unregisters a player by the given address. *