When your iPhone app crashes with ‘BAD ACCESS’ you’re in trouble – a memory bug where you tried to call a method on a object that was already deleted. Instruments has support for NSZombie – a feature that makes it easy to find the source of the bug by showing you a full history of every alloc, retain, release, and autorelease of the object that caused the crash! Wow. Here’s how:
Basic steps are:
- Run in Performance tool ‘Object Allocations’
- Stop running and set options on ObjAllocations instrument: ‘Enable NSZombie’ Detection and ‘Record Reference Counts’
- Re-run from instruments, when it crashes, click the arrow in the popup zombie dialog
- Open up the stack trace view and see the full memory history of the problem object
- Wow, how awesome is that?
- If you love this post, do me a favor and check out our Free app Focus for Facebook
- You might also like my memory management tutorial and other posts under ‘App Development’.
Here’s a link to the demo program: ZombieDebug Demo Project. The code we’re looking at in the video is:
@implementation ZombieDebugViewController
@synthesize objArray;
-(void)rewriteText {
NSMutableString* s = [NSMutableString stringWithCapacity:100];
for (id obj in objArray) {
[s appendFormat:@"%@,\n",obj];
[obj release];
}
label.text = s;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.objArray = [NSMutableArray arrayWithCapacity:10];
[objArray addObject:@"I'm a string object"];
[self rewriteText];
}
-(IBAction) tapButton:(id)button {
NSNumber* n = [NSNumber numberWithLong:random()];
[objArray addObject:n];
[self rewriteText];
}
-(void)dealloc {
[super dealloc];
self.objArray=nil;
}
@end

Xcode has changed the interface a bit but still a great video.
Thanks.
Thank you very much for taking your time to help me by posting this nice tutorial. Double clicking on an item in the stack (right column) does not take me to a line of code in Xcode (as it works in your tutorial). Instead it takes me to a memory address and what look like CPU instructions (mov, call, jmp, nop). I would like Instruments to go to the corresponding line of code in Xcode when I double click an item on the stack. I have the Xcode Preferences > General section set to “All-In-One” as suggested.
Also, I have noticed that the items in the stack (right column) are somewhat greyed.
Working with Xcode 3.2.4 / Instruments 2.7. Thanks for any help.
Mark
Brilliant. I will be using this all the time from now on. Can’t remember how many hours of tracking down these kinds of errors.
Jeremy
@jeremybassett
Hi,thanks for the blog post. Infos are pretty usefull and saved me many time which I spend on something else instead of searching :) Im waiting for more, bye :)
- Zoie GRISHAM
This just ended a 16 hour debugging session for me.
Thanks!
You saved me HOURS of time! stringWithString followed by a release, code that some other developer wrote and it was blowing up in my code in a completely different module.
THANK YOU!
Hi, just to say : Thank you man! :)
You rock Mark!
Hope you guys are doing well, sounds like you are.
-Mike H.
You can’t get tired of hearing how awesome this video is… So, here goes again….
This is just great… Solved in minutes what I’ve been chasing after for hours…
Wow, that tutorial saved me loads of time and hurt…
thank you, very nice, clear and concise tutorial on something that causes headaches to not few developers.
Freakin’ lovely tutorial… Thanks for saving me hours of trying to track down my poor memory management skills
You. Are. Amazing.
Muchas Gracias!
Its short and sweet explanation about memory leaks.
Thank you.
It’s very good!
@Paul Lohr! You have just set product-edit-scheme set profile from distribution to debug!
Now you see your symbols as code…
;-)
I finally find this (thanks a lot, btw) and Apple decides to introduce ARC.
Thank you, thank you, thank you, thank you…. ad infinitum