2006/2/4, Caleb Clausen <vikkous / gmail.com>:
> 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.)

You're right - I don't. Object#__id__ returns an object id.

> 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 would not count on IRB in such circumstances - especially if local
variables are involved. IRB does certain things differently there. Did
you only test in IRB or also in a Ruby script?

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

Not so fast. This error you are seeing is absolutely expected:
i.__getobj__ returns the original instance. If that is not an object
id (which it isn't in your case) it's not a legal argument for
ObjectSpace._id2ref().

You probably wanted o2=i.__getobj__

Since you keep a reference to o all the time in the block,
ObjectSpace._id2ref must always return the same instance. *If* you
actually see the error you claimed you saw initially then there's
something seriously broken.  At the moment I rather suspect it's some
other issue (such as testing in IRB).  I'd also try to use brackets
around the equality test - just to be sure that precedence doesn't
come into play.

robert

--
Have a look: http://www.flickr.com/photos/fussel-foto/