Errors from `ObjectSpace._id2ref`

回眸只為那壹抹淺笑 提交于 2019-12-24 01:01:27

问题


What is the difference between the following two kinds of errors that ObjectSpace._id2ref returns?

0x... is recycled object (RangeError)

0x... is not id value (RangeError)

回答1:


not id value means that there never was an object with that id.

recycled object means that there once was an object with that id, but it has been garbage collected.

Demo on Ruby 1.9.3/Ubuntu:

x = Object.new
id = x.object_id

puts "0x%x" % id
# => 0x4aef5e8

puts ObjectSpace._id2ref id
# => #<Object:0x95debd0>

x = nil

puts ObjectSpace._id2ref id
# => #<Object:0x95debd0>

GC.start

puts ObjectSpace._id2ref id
# => 0x4aef5e8 is recycled object (RangeError)

Note that the number in Object#to_s is not the object_id - according to the docs it is "an encoding of the object id".



来源:https://stackoverflow.com/questions/13508536/errors-from-objectspace-id2ref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!