iPhone Memory Debugging with NSZombie and Instruments

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:

  1. Run in Performance tool ‘Object Allocations’
  2. Stop running and set options on ObjAllocations instrument: ‘Enable NSZombie’ Detection and ‘Record Reference Counts’
  3. Re-run from instruments, when it crashes, click the arrow in the popup zombie dialog
  4. Open up the stack trace view and see the full memory history of the problem object
  5. Wow, how awesome is that?
  6. If you love this post, do me a favor and check out our Free app Focus for Facebook
  7. You might also like my memory management tutorial and other posts under ‘App Development’.

Enable Zombies in Instruments

Enable Zombies in Instruments

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

Comments

  1. Grant Morgan says:

    Xcode has changed the interface a bit but still a great video.
    Thanks.

  2. Paul Lohr says:

    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.

  3. 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

  4. StephanyROHR says:

    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

  5. 0x90 says:

    This just ended a 16 hour debugging session for me.
    Thanks!

  6. 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!

  7. Dragan says:

    Hi, just to say : Thank you man! :)

  8. Mike Hanna says:

    You rock Mark!

    Hope you guys are doing well, sounds like you are.

    -Mike H.

  9. NewDev says:

    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…

  10. cjr says:

    Wow, that tutorial saved me loads of time and hurt…

  11. herr ernst says:

    thank you, very nice, clear and concise tutorial on something that causes headaches to not few developers.

  12. Benjamin says:

    Freakin’ lovely tutorial… Thanks for saving me hours of trying to track down my poor memory management skills

  13. Dunc says:

    You. Are. Amazing.

    Muchas Gracias!

  14. lakki says:

    Its short and sweet explanation about memory leaks.

    Thank you.

  15. kasv6 says:

    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…

    ;-)

  16. Rod says:

    I finally find this (thanks a lot, btw) and Apple decides to introduce ARC.

  17. Rod says:

    Thank you, thank you, thank you, thank you…. ad infinitum

Trackbacks

  1. iWyre says:

    [...] Mark Johnson has put together a little video showing how to use Instrument’s NSZombie support right here. ©2008-2010 Jeff LaMarche. http://iphonedevelopment.blogspot.com [...]

  2. [...] iPhone Memory Debugging with NSZombie and Instruments | markjnet. [...]

  3. [...] iPhone Memory Debugging with NSZombie and Instruments A handy overview of why it’s important to use Instruments to help you debug memory issues on the iPhone. [...]

  4. [...] 15, 2010 · Leave a Comment Found this great little debugging tip over at MarkJ.net. The short of it: You can use NSZombie tracking to debug memory crashes in your code. Great find [...]

  5. [...] iPhone Memory Debugging with NSZombie and Instruments | markjnet (tags: iPhone debugging Instruments)   « links for 2010-02-02 |   [...]

  6. [...] iPhone Memory Debugging with NSZombie and Instruments | markjnet (tags: iPhone debugging Instruments)   « links for 2010-02-12 |   [...]

  7. [...] you access an object that has vanished into thin air, most likely because you over-released it. This screencast from Mark Johnson shows you how to debug these errors with Instruments and NSZombies. You’ll [...]

  8. [...] and found this wonderful post that led me in the right direction. @markjnet‘s post on zombie use detection was spot on and helped me find my bug within an hour of using the [...]

  9. [...] site http://www.markj.net qui traite du sujet avec une petite vidéo tutoriel à l’appuie. [...]

  10. [...] 其实还可以在Instruments中开启NSZombie选项,这样就可以在Instruments中直接查看crash时候的call stack了:http://www.markj.net/iphone-memory-debug-nszombie/ [...]

  11. [...] 사용하는 방법 : http://www.markj.net/iphone-memory-debug-nszombie/ Xcode4에서는 Product > Profile로 바뀌었다. 좀비모드, allocation모드, leak모드도 [...]

  12. [...] 文祥iphone iphone, 调试 No Comments 原文:http://www.markj.net/iphone-memory-debug-nszombie/ [...]

Speak Your Mind

*