On 2/4/06, Robert Klemme <bob.news / gmx.net> wrote:
> I'm a bit confused: where are the WeakReferences your subject mentions?
> Also, on my 1.8.3 on cygwin this runs without a problem.  If the code throws
> then I presume there is a problem with the Ruby interpreter you use
> (platform induced int overflow?).

The call to __id__ creates the weak reference. Anyway, I consider it a
weak reference, even though there's no WeakRef involved; perhaps you
don't. (__id__ is what WeakRef uses internally.)

I now see that I also get the problem with my ruby 1.6 _if_ I run the
test program within irb; without irb, it runs without problems.

I've also tried a variant that creates an actual WeakRef (calling
WeakRef.new and #__getobj__ instead of __id__ and
ObjectSpace._id2ref); it does not (AFAICT) get the same error, but
instead a different one, which also seems like it shouldn't happen.
Here's the modified script:


'require 'weakref'
100_000.times{|n|
  o=Object.new;
  i=WeakRef.new o;
  o2=ObjectSpace._id2ref(i.__getobj__);
  o.equal? o2 or raise "o=#{o}, i=#{"%x"%i}, o2=#{o2.inspect}, n=#{n}"
}

And the error I get:
weakref_bug.rb:5:in `_id2ref': cannot convert Object into Integer (TypeError)
        from weakref_bug.rb:5
        from weakref_bug.rb:2:in `times'
        from weakref_bug.rb:2

I agree that it does seem to be a problem with the interpreter.