Re: NSenu and memory ownership
Re: NSenu and memory ownership
- Subject: Re: NSenu and memory ownership
- From: Caleb Strockbine <email@hidden>
- Date: Fri, 25 Jul 2008 15:05:28 -0400
On Friday, July 25, 2008, at 01:43PM, Wesley Smith wrote:
>I have a question regarding memory ownership and NSMenus. Let's say I
>have an application with a nib file containing the usual menu items
>like File, Edit, Window etc. If, during the execution of my app, I
>swap out the Window menu with another NSMenu object generated during
>runtime, who owns what memory. Do I take ownership of the NSMenu
>object that used to be the Window menu and thus have to free it later?
There's nothing special about NSMenus with respect to memory management --
just follow the same oft-repeated rules you would use with any objective-C object.
Those rules are here:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concepts/ObjectOwnership.html#//apple_ref/doc/uid/20000043
Removing a menu with -removeItem: or -removeItemAtIndex: doesn't return
anything, and in any case you didn't alloc, copy, or retain anything, so you
don't have to worry about releasing anything. There's an example here:
http://developer.apple.com/documentation/Cocoa/Conceptual/MenuList/Articles/RemovingMenu.html
> Does the AppMenu NSMenu take ownership of the newly created Window
>NSMenu, freeing me from having to free it? How does this play out?
If you create the menu, then you'll need to release it when you're done with it,
i.e. after you insert it into the main menu. You don't need to worry about what
the main menu does with it when you insert it; that's not your business. It might
retain the menu, or it might make a copy, or it might do something entirely
different.
cheers,
Caleb.
_______________________________________________
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