Checking Malloc History

Sometimes it is useful to see what malloc operations have been performed by a given application. Doing this is a fairly straightforward process that can be facilitated with the command malloc_history, but requires a little bit of setup for iPhone-specific apps since the console cannot be used directly.

Go under the Executables directory of your project in XCode.

“Get Info” on your application.

Click on the “Arguments” tab

Under Environment variables set MallocStackLogging and NSZombieEnabled to YES. The former allows for malloc_history to be used, the latter logs deallocated memory, which can help find the memory address.

Run the app using the debugger (Command-Option-Y)

Check the console or probe around with the debugger to find the memory address you want to check.

Go to top of the Debugger Console and look for a line that looks like this:

launchd(28861) malloc: stack logs being written into /tmp/stack-logs.28861.launchd.7lhXSV

Taking the pid from that statement (28861) in this case and the memory address you picked up earlier (from, for example, a debug output line that looks something like this: -[NGTableViewDelegate tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x104f650 or from otherwise looking around with the debugger).

Type malloc_history
the command line or shell malloc_history
from inside of GDB.