Delegation: should be weak-linked, yet Delegating-Clients... strong linked?
Delegation: should be weak-linked, yet Delegating-Clients... strong linked?
- Subject: Delegation: should be weak-linked, yet Delegating-Clients... strong linked?
- From: "Frederick C. Lee" <email@hidden>
- Date: Wed, 12 Jan 2011 10:46:25 -0800
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'?
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?
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