ruby-core

Mailing list archive

[ruby-core:66979] [ruby-trunk - Bug #10620] #define_singleton_method keeps object from being garbage collected

From: sean@...
Date: 2014-12-19 15:06:00 UTC
List: ruby-core #66979
Issue #10620 has been updated by Sean Dilda.


Thanks for the quick response.

I tried your code and saw what you did.. it cleans up the objects, just a lot slower than I expected.
Please feel free to close out this bug.  I'd do so, but I'm not seeing the option.

----------------------------------------
Bug #10620: #define_singleton_method keeps object from being garbage collected
https://bugs.ruby-lang.org/issues/10620#change-50511

* Author: Sean Dilda
* Status: Open
* Priority: Normal
* Assignee: 
* Category: core
* Target version: 
* ruby -v: 2.1.5
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
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.



-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next