• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: how to create a NSMenuItem without memory leaks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to create a NSMenuItem without memory leaks


  • Subject: Re: how to create a NSMenuItem without memory leaks
  • From: Martin Batholdy <email@hidden>
  • Date: Tue, 17 May 2011 01:44:11 +0200

> Also, you are double-initializing the object, and under no circumstances should you ever do that.

Ok, thanks for pointing that out ... I still overlook these things ...


Ok ... now I tried to reread the official documents ...


Is this how you would do it properly:

newItem = [[NSMenuItem alloc]  initWithTitle:aTitle action:@selector(show:)  keyEquivalent:@""];

[menu addItem:newItem];
// with the addItem command newItem gets retained by 1. So I can release it:

[newItem release];


and [menu removeItemAtIndex: x]; should destroy the object newItem points to because the retain count decreases to 0.






On 17.05.2011, at 01:13, Nick Zitzmann wrote:

>
> On May 16, 2011, at 5:04 PM, Martin Batholdy wrote:
>
>> Hi,
>>
>> I create an NSMenu by myself;
>>
>> menu = [[NSMenu allocWithZone:menuZone] init];
>>
>> And depending on some data NSMenuItems get created.
>>
>>
>> Now these menuItems get deleted and redrawn depending on some actions of the user with
>>
>> [menu removeItemAtIndex: x];
>>
>>
>> I create the menu items like this:
>>
>> newItem = [[NSMenuItem alloc] init];
>> [newItem initWithTitle:aTitle
>> 					action:@selector(show:)
>> 			 keyEquivalent:@""];
>> [menu addItem:newItem];
>>
>>
>> Now is this a memory leak?
>
> Yes, unless you turned on GC. Also, you are double-initializing the object, and under no circumstances should you ever do that.
>
>> Because I don't release it anywhere. However the menu sometimes gets updated.
>> So this code ([[NSMenuItem alloc] init]) is invoked several times without a release call anywhere.
>>
>>
>> Is this better:
>>
>> NSMenuItem *newItem = [menu addItemWithTitle:aTitle action:@selector(show:) keyEquivalent:@""];
>> ...
>>
>> since I don't own newItem it should get autoreleased, right?
>
> Please re-read the memory management rules I sent you last time. The answer is in there.
>
> Nick Zitzmann
> <http://www.chronosnet.com/>
>

_______________________________________________

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

  • Follow-Ups:
    • Re: how to create a NSMenuItem without memory leaks
      • From: Conrad Shultz <email@hidden>
References: 
 >how to create a NSMenuItem without memory leaks (From: Martin Batholdy <email@hidden>)
 >Re: how to create a NSMenuItem without memory leaks (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Re: selectionIndexesForProposedSelection on mouse Up or mouse Down
  • Next by Date: Re: how to create a NSMenuItem without memory leaks
  • Previous by thread: Re: how to create a NSMenuItem without memory leaks
  • Next by thread: Re: how to create a NSMenuItem without memory leaks
  • Index(es):
    • Date
    • Thread