3

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)
sawa
  • 165,429
  • 45
  • 277
  • 381

1 Answers1

3

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".

Andrew Haines
  • 6,574
  • 21
  • 34
  • I understand your explanation but do not understand your example. Why is the RangeError message showing a different object_id than the previous one? – sawa Nov 22 '12 at 14:40
  • Ah, yeah, I overlooked that, sorry. Updated accordingly. – Andrew Haines Nov 22 '12 at 20:19