为什么ObjectSpace._id2ref在Ruby1.9和Ruby2.0上提供不同的输出?
Ruby1.9.3p392 i 386-mingw32
class Foo ; end
Foo.object_id #=> 17569464
ObjectSpace._id2ref(17569464) #=> Foo
Foo.new.singleton_class.object_id #=> 17075124
ObjectSpace._id2ref(17075124) #=> "\x00"Ruby2.0.0p0i 386-mingw32
class Foo ; end
Foo.object_id #=> 17197176
ObjectSpace._id2ref(17197176) #=> Foo
Foo.new.singleton_class.object_id #=> 19181436
ObjectSpace._id2ref(19181436) #=> RangeError: 0x124af7c is recycled object
Foo.new.singleton_class.object_id #=> 17454324
ObjectSpace._id2ref(17454324) #=> RangeError: 0x10a54f4 is not id value
Foo.new.singleton_class.object_id #=> 17139816
ObjectSpace._id2ref(17139816) #=> "c"发布于 2013-03-17 08:24:40
仅仅是因为在2.0中垃圾收集器很容易出错。
# RangeError: 0x124af7c is recycled object对象的状态已经被GC‘编辑了。
UPD:我们可以使用Mutex来处理请求的行为
2.0.0 (main):0 > Mutex.new.synchronize {
2.0.0 (main):0 * class Foo ; end
2.0.0 (main):0 * id = Foo.new.singleton_class.object_id
2.0.0 (main):0 * puts id
2.0.0 (main):0 * puts ObjectSpace._id2ref(id)
2.0.0 (main):0 * }
# 23172260
# <Class:#<Foo:0x00000002c32970>>https://stackoverflow.com/questions/15458861
复制相似问题