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: "Frederick C. Lee" <email@hidden>
- Date: Wed, 12 Jan 2011 13:51:47 -0800
I suspect that to be the case when I received this code for study.
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'?
return self;
}
On Jan 12, 2011, at 12:45 PM, Corbin Dunn wrote:
> 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
If I understand you right, NSWindowController is a client (instantiated within) the NSApplication and hence, should be retained.
Yet, NSWindowConroller is a delegate of NSWindow, so it should be assigned to (vs retained by) NSWindow.
NSApplication ---> [NSWindowController retain] <----- delegate [NSWindowController assign] ---- NSWindow.
This setup has a broken cycle, with the NSWindowController strongly linked to NSApplication, yet weakly linked (as a delegate) to its host.
In my case (above) there's no delegate client, merely an object with its delegate and hence, should be weakly linked via assign.
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