Re: Delegation: should be weak-linked, yet Delegating-Clients... strong linked?
Re: Delegation: should be weak-linked, yet Delegating-Clients... strong linked?
- Subject: Re: Delegation: should be weak-linked, yet Delegating-Clients... strong linked?
- From: Corbin Dunn <email@hidden>
- Date: Wed, 12 Jan 2011 12:45:34 -0800
On Jan 12, 2011, at 10:46 AM, Frederick C. Lee wrote:
> Environment: OS X (10.6+)
>
> Greetings:
> I've always thought to weak-link delegate methods to their hosts. Hence the use of 'assign' vs 'retain' delegate assessors.
> Quoting Apple's Delegate documentation:
>
> "Delegating objects do not (and should not) retain their delegates.
> However, clients of delegating objects (applications, usually) are responsible for ensuring that their
> delegates are around to receive delegation messages.
> To do this, they may have to 'retain' the delegate in memory-managed code."
>
> Source: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html
>
>
> However upon viewing some code, I encountered a hard-link to a delegate (via 'retain').
>
> Scenario: MyWindowController:NSWindowController -----> MyViewController:NSViewController (a delegate)
>
>
>
> Inside 'MyViewController':
>
> - (id)initWithSerial:(NSString *)_serial email:(NSString *)_email delegate:(NSObject <MyControllerProtocol>*)_delegate {
> self = [super init];
> serial = [_serial retain];
> email = [_email retain];
> //!!!: Delegating objects do not (and should not) retain their delegates.
> // However, clients of delegating objects (applications, usually) are responsible for ensuring that their delegates are around to receive delegation messages.
> // Is 'MyController' a client of a delegating object?
> delegate = [_delegate retain]; // ...should this be 'retain' or 'assign
probably assign
> '?
> return self;
> }
>
> I'm a bit confused here. On one hand, "...delegating objects should not retain their delegates."
> ... but on the other hand, "...clients of delegating objects...SHOULD retain their delegates."
>
> Question: what is considered a 'client' of a delegating object?
Clients of delegating objects are the things that hold onto the delegating objects. So, for example, if you have a bunch of NSWindowController instances those objects are usually delegates for NSWindows. Now, the NSWindowControllers need someone to hold onto them. Typically your global NSApplication object (or something else). That object is the client of the NSWindowController, and needs to retain the NSWindowControllers.
All delegates (in general) should be "assign" to avoid cycles. In your particular case, your window controller will probably need to keep track of all of its NSViewController instances via a retain (maybe an NSArray). However, even if your NSWindowController is the delegate of the NSViewController, it should not be retained via the delegate (by convention).
corbin
>
> In the above case, we have only a NSWindowController object and its NSViewController delegate.
> I don't see any client. This is strictly a 1:1.
>
> Any thoughts?
>
> Regards,
> Ric.
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden