Monday, August 2, 2010

Finding Memory Leaks - the easy way

From the iPhone Development Guide, "Debugging Applications"


Finding Memory Leaks

If you fix a leak and your program starts crashing, your code is probably trying to use an already-freed object or memory buffer. To learn more about memory leaks, see "Finding Memory Leaks".

You can use the NSZombieEnabled facility to find the code that accesses freed objects. When you turn on NSZombieEnabled, your application logs accesses to deallocated memory, as shown here:

2008-10-03 18:10:39.933 HelloWorld[1026:20b] *** -[GSFont ascender]: message sent to deallocated instance 0x126550

To activate the NSZombieEnabled facility in your application:

  1. Choose Project > Edit Active Executable to open the executable Info window.
  2. Click Arguments.
  3. Click the add (+) button in the “Variables to be set in the environment” section.
  4. Enter NSZombieEnabled in the Name column and YES in the Value column.
  5. Make sure that the checkmark for the NSZombieEnabled entry is selected.
For more information about configuring executable environments, see “Configuring Executable Environments” in Xcode Project Management Guide.

iPhone 3.1 SDK introduces the Leaks instrument, which you can use in the Instruments application to easily find memory leaks. For more information, see Instruments User Guide.

© 2010 Apple Inc. All Rights Reserved. (Last updated: 2010-05-28)

No comments:

Post a Comment