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. You saved my day! Many thanks for pointing out how to see the stack trace info. I’ve downloaded the Focus app in gratitude.

  2. Marc says:

    Just wanted to say thanks for making this video, it was a great help and helped me fix my memory error. I learned something new. :)

  3. Moody says:

    Awesome,this tutorial saved me!!

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/ [...]

  13. [...] followed the instructions here. Making sure the extended view was open, I was able to determine the exact line of code that had [...]

  14. [...] 用NSZombieEnabled解决恼人的EXC_BAD_ACCESS错误 NSZombieEnabled iPhone Memory Debugging with NSZombie and Instruments Finding Messages Sent To Deallocated Objects This entry was posted in IOS and tagged [...]

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

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

Speak Your Mind

*