Actions
Bug #10620
closed#define_singleton_method keeps object from being garbage collected
Description
After using #define_singleton_method on an object, the object is never garbage collected
Sample code:
#!/usr/bin/env ruby
obj = Object.new
obj_id = obj.object_id
obj = nil
GC.start
GC.start
GC.start
GC.start
begin
ObjectSpace._id2ref obj_id
puts "GC failed, object still exists"
rescue RangeError
puts "GC worked"
end
obj = Object.new
obj.define_singleton_method(:f) { "F" }
obj_id = obj.object_id
obj = nil
GC.start
GC.start
GC.start
GC.start
begin
ObjectSpace._id2ref obj_id
puts "GC failed, object still exists"
rescue RangeError
puts "GC worked"
end
This outputted:
GC worked
GC failed, object still exists
The object without the singleton method was cleaned up properly, the object with defined_singleton_method used never got cleaned up.
Actions
Like0
Like0Like0Like0Like0