From: Robert Klemme Date: 2006-02-05T20:08:20+09:00 Subject: Re: Problem with weak references on OS X 10.3 ------=_NextPart_000_002E_01C62A4C.AF75C590 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit Caleb Clausen wrote: > On 2/4/06, Robert Klemme wrote: >>> 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? > > It happens running it with plain ruby (no irb) on my ruby 1.8 (and > 1.9). I only mentioned it because irb does seem to be required to > create the problem on my ruby 1.6. Irb is not the problem; it doesn't > treat local variables that differently. > >> 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__ > > Uh-oh. You're right. Too much monkey code and hack, not enough look > and think. > > (After hurriedly fixing my test...) Ok, so if I _correctly_ use > WeakRefs, there is no problem. That is interesting, and I'd sure like > to know why, because it's not obvious to me. I'm going to investigate > this deeper, and see if I isolate the difference that lets WeakRef > work. > >> 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. > > I'm pretty sure about the precedence of or, but just in case, I tried > it with more parens. It's still broken. I was also, but sometimes it's better to explicitely rule potential sources of error out. I have to admit I still cannot believe that you actually saw the results you claimed to see initially. Can anybody verify this on Mac OS please? I don't have a Mac around otherwise I'd do it. I've attached an equivalent version of the script. Kind regards robert ------=_NextPart_000_002E_01C62A4C.AF75C590 Content-Type: application/octet-stream; name="idref.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="idref.rb" #!/usr/bin/env ruby=0A= =0A= print( (ARGV.shift || 1000).to_i.times do |i|=0A= o1 =3D Object.new=0A= id =3D o1.object_id=0A= o2 =3D ObjectSpace._id2ref id=0A= o1.equal? o2 or raise "#{i}: #{o1.inspect} #{o2.inspect} - #{id} = #{o2.object_id}"=0A= end, " runs\n" )=0A= ------=_NextPart_000_002E_01C62A4C.AF75C590--