From f3b8f8461a045b8b3d6d5a8a46c71d519ff1d64c Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 1 Apr 2015 12:44:30 +1100 Subject: [PATCH] FIX: severe memory leak in WeakValueMap entries were not being cleaned up correctly from the backing store --- lib/v8/weak.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/v8/weak.rb b/lib/v8/weak.rb index 46147980..ee39d37d 100644 --- a/lib/v8/weak.rb +++ b/lib/v8/weak.rb @@ -39,7 +39,16 @@ def [](key) end def []=(key, value) - @values[key] = V8::Weak::Ref.new(value) + ref = V8::Weak::Ref.new(value) + ObjectSpace.define_finalizer(value, self.class.ensure_cleanup(@values, key, ref)) + + @values[key] = ref + end + + def self.ensure_cleanup(values,key,ref) + proc { + values.delete(key) if values[key] == ref + } end end end