首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么`ObjectSpace._id2ref`在Ruby1.9和Ruby2.0上提供不同的输出?

为什么`ObjectSpace._id2ref`在Ruby1.9和Ruby2.0上提供不同的输出?
EN

Stack Overflow用户
提问于 2013-03-17 08:21:33
回答 1查看 242关注 0票数 0

为什么ObjectSpace._id2ref在Ruby1.9和Ruby2.0上提供不同的输出?

Ruby1.9.3p392 i 386-mingw32

代码语言:javascript
复制
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

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-17 08:24:40

仅仅是因为在2.0中垃圾收集器很容易出错。

代码语言:javascript
复制
# RangeError: 0x124af7c is recycled object

对象的状态已经被GC‘编辑了。

UPD:我们可以使用Mutex来处理请求的行为

代码语言:javascript
复制
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>>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15458861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档