问题
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