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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to