Re: menu madness with retain count
Re: menu madness with retain count
- Subject: Re: menu madness with retain count
- From: Charles Srstka <email@hidden>
- Date: Tue, 27 Apr 2010 19:09:29 -0500
On Apr 27, 2010, at 3:49 PM, Bill Appleton wrote:
> if i add a submenu to an item then it is retained by the menu
>
> but then if i then set that submenu to nil is it still retained?
>
> if retained count was accurate you could test it on final release, etc
Well, the direct answer is no, since -[NSMenuItem setSubmenu:] is just an accessor method, most of which in Cocoa look something like this:
- (void)setSubmenu:(NSMenu *)submenu {
if(someIvar != submenu) {
[someIvar release];
someIvar = [submenu retain];
// maybe do some other stuff
}
}
As you can see, passing nil to a setter in a Cocoa object typically causes the existing ivar to be released, before the new one is set to nil. There are, of course, variations on this - the method could use -autorelease instead of -release, it could use -copy instead of -retain, it could simply be a @synthesized setter in Mac OS X 10.5 and higher, etc. But the one thing that is certain is that you don’t have to worry about this sort of thing. If NSMenuItem was the object that retained the submenu, then NSMenuItem is the object that has responsibility for releasing it, not you. Once you pass the menu to NSMenuItem, you can be sure that NSMenuItem will do the right thing with it, and the only way you need to worry about it being released is if *you* are retaining it. If NSMenuItem were failing to release the submenu, that would be a bug in NSMenuItem, and not your problem. At this stage in Cocoa’s maturity, such a bug in such a basic class would be quite unlikely, but even assuming such a bug did exist, if you attempted to “fix” the bug by releasing the submenu yourself, then your app would crash as soon as Apple fixed the bug with a software update.
Charles_______________________________________________
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