Previous Entry Add to Memories Tell a Friend Next Entry
Checking Mac OS X applications for memory leaks
userpic
[info]chanson
A developer on the comp.lang.objective-c newsgroup asked recently how to check his Cocoa application for memory leaks.

Here's a simple process for using the leaks(1) command-line tool to investigate leaks in your applciation.
  1. Add some code to your application to wait instead of exiting, such as an atexit(3) callback that just sleeps:

      while (1) sleep(60);

    This ensures that your application stays around after it has otherwise completley shut down.

  2. Launch your application with the MallocStackLogging environment variable set to 1. You can set this by inspecting the executable — not the target — for your application. (See the malloc(3) manpage for more details on the environment variables that you can use with it.)

  3. Launch your application from within Xcode, run through a series of actions in your application, and quit your application. It should now be paused in your atexit(3) callback.

  4. Run /usr/bin/leaks against your sleeping application by specifying your sleeping application's process ID as its argument.

This will give you a list of blocks that leaks(1) thinks have been leaked from your application, their contents, and — thanks to the MallocStackLogging environment variable — the stack trace at the point each leaked block was allocated!

You can even do post-processing on this output to do things like format the stack traces and automatically file bugs against your application. Heck, you could even have your application have a debugging mode that launches leaks(1) And the best part: This isn't specific to Cocoa. It will work for Cocoa, Carbon, and even Unix tools. Anything that uses the standard system malloc(3) can be checked this way.

(Leave a comment)

Thanks for the gift!

(Anonymous)

2006-07-03 05:13 am (UTC)

Chris,
Thank you very much for figuring this out and making it comprehensible. I'm working on my first real Cocoa app and was perplexed about how I was going to deal with this issue in a few months. I'm most grateful!
Joel

Isn't what malloc debug or OmniObjectMeter are for ?

(Anonymous)

2006-07-03 06:31 am (UTC)

malloc debug is probably just a UI over leaks binary, but it is simple and free, bundled with Xcode... I think it is much more readable that leaks outputs...

Aurelien

Need help making an executable intentionally leak

(Anonymous)

2008-03-07 05:29 pm (UTC)

I have written a "Hello World" Cocoa application with a memory leak intentionally built in just to try out Instruments, MallocDebug, leaks, etc. It should leak, but none of these tools show it leaking. I must be doing something wrong. Is there an example available of a test application with an intentional leak?

Memory Leak example

(Anonymous)

2008-12-25 07:16 pm (UTC)

An example of a memory leak in C is...

#include
[Error: Irreparable invalid markup ('<stdlib.h>') in entry. Owner must fix manually. Raw contents below.]

An example of a memory leak in C is...

#include <stdlib.h>

int main(){
void* bob=malloc(12);//the memory is never deallocated
return 0;
}

In cocoa...

NSString *str=[[NSString alloc] init];

//PROGRAM ENDS HERE WITH GARBAGE COLLECTION OFF

(Leave a comment)