PERF: Fix memory leak

We used to leak some memory every time you cook a post

see: https://github.com/cowboyd/therubyracer/pull/336
This commit is contained in:
Sam 2015-04-01 13:30:38 +11:00
parent 3d0c03bfb0
commit d390603274
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
## TODO: DELETE ME WHEN https://github.com/cowboyd/therubyracer/pull/336
# is upstreamed and released
#
module V8
module Weak
class WeakValueMap
def initialize
@values = {}
end
def [](key)
if ref = @values[key]
ref.object
end
end
def []=(key, value)
ref = V8::Weak::Ref.new(value)
ObjectSpace.define_finalizer(value, self.class.ensure_cleanup(@values, key, ref))
@values[key] = ref
end
private
def self.ensure_cleanup(values,key,ref)
proc {
values.delete(key) if values[key] == ref
}
end
end
end
end