From dac96abd240b66f747ea01260207ce72f20dcef3 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Mon, 10 Jul 2023 04:41:09 -0400 Subject: [PATCH] HHH-16911 MapBackedClassValue ClassLoader leak --- .../util/collections/MapBackedClassValue.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/MapBackedClassValue.java b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/MapBackedClassValue.java index 190dfa457f..a6878087e5 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/MapBackedClassValue.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/MapBackedClassValue.java @@ -40,7 +40,7 @@ public final class MapBackedClassValue { public MapBackedClassValue(final Map, V> map) { //Defensive copy, and implicit null check. //Choose the Map.copyOf implementation as it has a compact layout; - //it doesn't have great get() performance but it's acceptable since we're performing that at most + //it doesn't have great get() performance, but it's acceptable since we're performing that at most //once per key before caching it via the ClassValue. this.map = Map.copyOf( map ); } @@ -50,17 +50,17 @@ public final class MapBackedClassValue { } /** - * Use this to wipe the backing map, but N.B. - * we won't be clearing the ClassValue: this is useful - * only to avoid classloader leaks since the Map - * may hold references to user classes. - * Since ClassValue is also possibly caching state, - * it might be possible to retrieve some values after this - * but shouldn't be relied on. - * ClassValue doesn't leak references to classes. + * Use this to wipe the backing map, important + * to avoid classloader leaks. */ public void dispose() { + Map, V> existing = this.map; this.map = null; + if ( existing != null ) { + for ( Map.Entry, V> entry : existing.entrySet() ) { + this.classValue.remove( entry.getKey() ); + } + } } }