Re: menu madness with retain count
Re: menu madness with retain count
- Subject: Re: menu madness with retain count
- From: Graham Cox <email@hidden>
- Date: Wed, 28 Apr 2010 00:12:43 +1000
Bill, you need to learn about the concept of object ownership within Cocoa. Ownership is the model for memory management, retain count merely an implementation detail which can tell you nothing useful.
Menus own their submenus, so if you delete a menu with submenus, you don't need to also delete those submenus yourself. Chances are you are over-releasing them so when the parent menu is deleted, the submenu is no longer there and hence you get a crash.
You simply can't work with Cocoa without having a good solid understanding of memory management and particularly the concept of ownership. It's different from Carbon and different from C++ and so on. Your subject title is apt - using retain counts in an attempt to debug faulty memory management is the path to madness.
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
--Graham
On 27/04/2010, at 11:46 PM, Bill Appleton wrote:
> hi all,
>
> i like to create some menus, set them as the main menu bar, then maybe
> delete them, and create some more menus, use them, etc.
>
> but i can't find any way to do this with cocoa
>
> here is the problem
>
> when you add a submenu (setSubMenu:forItem) the retain count on the submenu
> goes up by one
>
> but when you delete that item, or set its submenu to nil, the retain count
> does not change
>
> if you ever delete a menu that has a submenu you will get a crash, but there
> is no way to unassociate the menu and the submenu
>
> another problem: when you set the main menu bar [NSApp setMainMenu:themenu]
> the retain count on themenu goes up by a lot, i can't predict it, but maybe
> 4 or 5
>
> if you set the main menu bar to nil the retain count does not change on the
> original main menu
>
> so given all this, i can't find a way to create and delete menus without
> crashes or memory leaks
>
> am i doing something wrong?
>
>
>
> thanks
>
> bill appleton
> _______________________________________________
>
> 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
_______________________________________________
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