Re: menu madness with retain count
Re: menu madness with retain count
- Subject: Re: menu madness with retain count
- From: Bill Appleton <email@hidden>
- Date: Tue, 27 Apr 2010 08:28:33 -0700
hi all,
i have read the memory rules a few times now, so does this sound right?
1) after i append an item i have created to a menu i have created, and i
don't want to own the menu item any more, i should release the item so that
the menu owns it
2) when i add a submenu i have created to a menu i have created, and i don't
want to own the submenu any more, i should release the submenu so that the
menu owns it
3) when i set the menus i have created for NSApp using setMainMenu then...
what? who owns them? how do i set more menus for NSApp? how do i get NSApp
to release the current set?
thanks,
bill
On Tue, Apr 27, 2010 at 7:12 AM, Graham Cox <email@hidden> wrote:
> 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