• 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: NSApplicationMain question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSApplicationMain question


  • Subject: Re: NSApplicationMain question
  • From: Charles Srstka <email@hidden>
  • Date: Sun, 25 Apr 2010 15:28:23 -0500

On Apr 25, 2010, at 8:44 AM, Bill Appleton wrote:

> 4) how do you guys check for memory leaks in the cocoa objects? when i
> delete a menu... do i have to delete the individual items?

The best thing to do is to read Apple’s Memory Management Guide, which will clear up a lot of things related to Cocoa memory management.

http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html

In a nutshell (and this is a simplistic generalization, so there are a few exceptions, but this covers the majority of cases):

1. If you used -alloc, -new, -retain, or -copy, or if you used a method starting with one of those (i.e. allocWithSomething:, newWithSomething:, etc.), you need to release the object when you’re done.

2. If you used some other method to get the object that doesn’t involve one of the above, for example -[NSString stringWithSomething:], then you don’t need to worry about releasing it.

3. When you get an object and store it as an instance variable, you should send -retain to it to make sure it sticks around at least as long as your object does, and then send -release (or -autorelease) when you’re done with it.

4. If you have a method that returns an object and doesn’t follow the naming convention in #1, you need to send -autorelease to the object before returning it, so that the caller doesn’t have to worry about releasing it.

In the case of the menus, all you need to be aware of is that if you create a menu item using -[[NSMenuItem alloc] initWithTitle:action:keyEquivalent:], then you should either autorelease it before giving it to an NSMenu, or release it after you give it. The NSMenu will retain the object so that it stays alive, and when you delete the menu, it will release all the individual items, so that if the menu is the only object that has them retained, they will be deleted.

Finally, to actually answer your question: To check for memory leaks, look at the Instruments application in the /Developer/Applications folder. It’s really powerful and gives you access to a lot of different tests for testing performance and memory leaks.

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

References: 
 >NSApplicationMain question (From: Bill Appleton <email@hidden>)
 >Re: NSApplicationMain question (From: Abhinay Kartik Reddyreddy <email@hidden>)
 >Re: NSApplicationMain question (From: Bill Appleton <email@hidden>)
 >Re: NSApplicationMain question (From: Shawn Erickson <email@hidden>)
 >Re: NSApplicationMain question (From: Bill Appleton <email@hidden>)
 >Re: NSApplicationMain question (From: Michael Ash <email@hidden>)
 >Re: NSApplicationMain question (From: Bill Appleton <email@hidden>)
 >Re: NSApplicationMain question (From: Jens Alfke <email@hidden>)
 >Re: NSApplicationMain question (From: Bill Appleton <email@hidden>)
 >Re: NSApplicationMain question (From: Jack Carbaugh <email@hidden>)
 >Re: NSApplicationMain question (From: Bill Appleton <email@hidden>)

  • Prev by Date: Not use of push-bottom method
  • Next by Date: Re: NSApplicationMain question
  • Previous by thread: Re: NSApplicationMain question
  • Next by thread: Re: NSApplicationMain question
  • Index(es):
    • Date
    • Thread