From: Garthy D Date: 2013-02-16T15:46:30+09:00 Subject: Re: Soft object reference for mark and sweep Hi Charlie, On 08/02/13 15:12, Charles Oliver Nutter wrote: > On Sun, Jan 27, 2013 at 8:21 AM, Garthy D > wrote: >> The WeakRef interface worries me though. You'd normally expect to see just >> one call on such a thing (lock, which turns weak to strong/fail) and maybe a >> check call, with a big warning that the result might change post-check. A >> delegation-style interface with a check only seems a bit unusual. However, I >> may just not have a proper understanding of how it works yet. > > I strongly discourage using WeakRef for its delegate interface. It's a > terrible pattern that simply needs to go away. If you use WeakRef, > just use it as a weak object holder and always check for nil on the > return value. You should be ok. > > You might also check out the "weakling" gem, which provides some other > nice features from JVM like reference queues, where your WeakRefs get > inserted as their references get collected. It's a bit more efficient > (or at least doesn't impact GC performance) than using finalizers. Thankyou for your thoughts on this one. Apologies for my slow reply- some time had elapsed and I've not got anything to notify me on new replies to the thread. I only just noticed this one. Doing some research on WeakRef revealed it had significant problems under MRI- not just with the Delegate interface, but that is sometimes got the object references wrong. That pretty-much rules it out for me. I didn't proceed using WeakRef in the end. This still isn't a solved problem for me so I can't say how I've overcome it yet, but if my current approach turns out then hopefully I'll have something interesting to report back. :) Cheers, Garth