Hello everyone, I've been doing some research into weak references in ruby and from what I've read it seems that Ruby's WeakRef implementation is both inefficient and unsafe. Here is the thread discussing the matter: http://redmine.ruby-lang.org/issues/show/4168 As Macruby has a different garbage collector I don't know if these problems are also present? I am working on a blocks based API for Key Value Coding and need to keep a weak reference to objects. Here is a very basic example of how this would look in garbage collected Objective-C: @interface Observer : NSObject { __weak id obj; } @implementation - (Observer *)initObservedObject:(id)object { obj = object; } Essentially I just need a Macruby equivalent of the above. Is there a simple way to have a weak referenced instance variable in Macruby? al
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 On Feb 14, 2011, at 5:48 AM, Alan Skipp wrote:
Hello everyone, I've been doing some research into weak references in ruby and from what I've read it seems that Ruby's WeakRef implementation is both inefficient and unsafe. Here is the thread discussing the matter: http://redmine.ruby-lang.org/issues/show/4168
As Macruby has a different garbage collector I don't know if these problems are also present?
I am working on a blocks based API for Key Value Coding and need to keep a weak reference to objects. Here is a very basic example of how this would look in garbage collected Objective-C:
@interface Observer : NSObject { __weak id obj; }
@implementation - (Observer *)initObservedObject:(id)object { obj = object; }
Essentially I just need a Macruby equivalent of the above. Is there a simple way to have a weak referenced instance variable in Macruby?
al _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
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
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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On Tue, Feb 15, 2011 at 5:28 PM, Laurent Sansonetti <lsansonetti@apple.com> 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.
Weak references and weak lists/hashes are often very useful for caches you need to age out or when you need to associate data with a given instance of an object without holding hard references. Using weak references is definitely not a design problem in itself. I'm also disappointed that you'd recommend people use NSHashTable rather than just fixing WeakRef :) In JRuby, we nuked weakref.rb some time ago because it has a lot of problems. Instead, we wrap the JVM's builtin weakrefs. See also the weakling gem, which (for JRuby) provides additional features mentioned in that Ruby redmine ticket like reference queues and a simple weak ID hash to use in place of _id2ref. - Charlie
participants (3)
-
Alan Skipp
-
Charles Oliver Nutter
-
Laurent Sansonetti