[MacRuby-devel] WeakRef advice

Alan Skipp al_skipp at fastmail.fm
Thu Feb 17 16:17:37 PST 2011


Hi Laurent,
Thanks for your suggestion - yes I do have control of the data structure, so I am able to implement it differently, I'll take a look at NSHashTable, I'll also consider if there is a way of avoiding this pattern.

I've did a bit more research and there seems to be another more underlying problem – Key Value Observing is actually easier to manage without garbage collection. It is allegedly not necessary to remove KVO observers when using garbage collection, but from my experiments the results are unpredictable.

Under certain circumstances explicitly cancelling observers is necessary, which is quite a pain, so my goal of making KVO simpler to use with Macruby becomes a bit more problematic.

al


On 15 Feb 2011, at 23:28, Laurent Sansonetti wrote:

> Hi Alan,
> 
> Do you control the data structure that holds a reference to 'B'? If yes, you may want to use NSHashTable which supports weak references.
> 
> To me, this sounds like a design problem. Maybe your project can be re-architectured to avoid this pattern.
> 
> Laurent
> 
> On Feb 15, 2011, at 12:22 AM, Alan Skipp wrote:
> 
>> Hi Laurent,
>> Thanks for the response. In essence the problem I have is something like this:
>> 
>> Object 'A' is created, then to handle Key Value Observing, Object 'B' is created which holds an instance variable to Object 'A'. 'B' is held in a hash or array somewhere. Object 'B' only has a purpose while 'A', is in use, when this is no longer the case I want 'B' to self destruct. However, 'B' is held in a hash and holds a reference to 'A', so neither object goes out of scope and neither can be garbage collected.
>> 
>> If I changed the implementation perhaps I could avoid this problem, though I'm not sure what the solution would be as yet.
>> 
>> al
>> 
>> On 14 Feb 2011, at 22:09, Laurent Sansonetti wrote:
>> 
>>> Hi Alan,
>>> 
>>> MacRuby should have the same problem. ObjectSpace._id2ref in MacRuby basically maps an object pointer value to a numerical description, and the GC recycles objects. 
>>> 
>>> I am curious why you need weak references, though. MacRuby is able to detect and deal with reference cycles, so you shouldn't need to worry about leaks. Is there another use case that I'm forgetting? 
>>> 
>>> Laurent
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20110218/70aa2b5e/attachment.html>


More information about the MacRuby-devel mailing list