Re: Delegate: to retain or not?
Re: Delegate: to retain or not?
- Subject: Re: Delegate: to retain or not?
- From: Ondra Cada <email@hidden>
- Date: Mon, 15 Jul 2002 16:28:07 +0200
On Monday, July 15, 2002, at 03:51 , petite_abeille wrote:
A awful doubt crossed my tired mind: should I retain my delegates or not?
Nope. The convention is not to retain them, on the presumption that the
delegate is generally a controller.
If I do, am I not creating "a clear and present danger" of retain cycles?
Exactly. Some very specific situations apart, the controller (which is
also the delegate) should retain its views and other helper objects (of
whose it is the delegate).
If I don't, should I use something along the line of [NSValue
valueWithNonretainedObject]? Or simply assign the delegate to my ivar?
Can I use NSIsFreedObject to check if the object is still around?
See above. The general pattern should be (conceptually, the actual code of
course tends to differ):
@implementation SomeController ...
-init... {
[foo retain];
[foo setDelegate:self];
[bar retain];
[bar setDelegate:self];
}
-(void)dealloc {
[foo setDelegate:nil];
[foo release];
[bar setDelegate:nil];
[bar release];
}
@end
Am I hopelessly confused?!?
Not really. The delegates are somewhat confusing, since there are good and
sound reasons for both approaches (to retain / not to retain), but the
convention I've just outlined was selected in NeXT ages ago as, so to
speak, the lesser evil. And, in my experience, it is quite good indeed: I'
ve got my share of retain/release problems of course, but never with
delegation.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.