Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

FIX: severe memory leak in WeakValueMap #336

Merged
merged 1 commit into from
Apr 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/v8/weak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what the reason to use proc {} ? Asking just for educational purpose

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@novokshonovi regarding the reasoning for the class method and rather awkward definition of finalizers, you need to be ultra careful with closures: see http://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/

values.delete(key) if values[key] == ref
}
end
end
end
Expand Down