From 5b21f4d61c36efb71c8547076dfb397e6cfe093c Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 10 Jun 2014 23:01:24 +0200 Subject: [PATCH] Don't cache equivalent converters in StructureModifier. This should hopefully fix a memory leak discovered by Stevenpcc (thanks). --- .../protocol/reflect/StructureModifier.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index 48794997..77319aa8 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -382,7 +382,6 @@ public class StructureModifier { */ @SuppressWarnings("unchecked") public StructureModifier withType(Class fieldType, EquivalentConverter converter) { - StructureModifier result = subtypeCache.get(fieldType); // Do we need to update the cache? @@ -405,7 +404,7 @@ public class StructureModifier { } // Cache structure modifiers - result = withFieldType(fieldType, filtered, defaults, converter); + result = withFieldType(fieldType, filtered, defaults); if (fieldType != null) { subtypeCache.put(fieldType, result); @@ -423,7 +422,6 @@ public class StructureModifier { if (!Objects.equal(result.converter, converter)) { result = result.withConverter(converter); } - return result; } @@ -464,7 +462,20 @@ public class StructureModifier { * @param fieldType - common type of each field. * @param filtered - list of fields after filtering the original modifier. * @param defaults - list of default values after filtering the original. - * @param converter - the new converter. + * @param converter - the new converter + * @return A new structure modifier. + */ + protected StructureModifier withFieldType( + Class fieldType, List filtered, Map defaults) { + return withFieldType(fieldType, filtered, defaults, null); + } + + /** + * Create a new structure modifier for the new field type. + * @param fieldType - common type of each field. + * @param filtered - list of fields after filtering the original modifier. + * @param defaults - list of default values after filtering the original. + * @param converter - the new converter, or NULL. * @return A new structure modifier. */ protected StructureModifier withFieldType( @@ -592,7 +603,6 @@ public class StructureModifier { result.add(field); } } - return result; } }