markjnet

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

[Slashdot] [Digg] [del.icio.us] [StumbleUpon]

Comments

31 Responses to “iPhone Memory Debugging with NSZombie and Instruments”
  1. Ted says:

    I spent 2 hours yesterday debugging one of these. I honestly had no idea this existed. This is an absolutely amazing tool, thanks for this video!

  2. namaz rahsa says:

    Thank you, for a very informative demo

  3. Robin says:

    Very cool, thanks a lot! I knew of NSZombie but did not know about this cool integration into instruments.

  4. Michael says:

    Very interesting, but I can’t make it work in Instruments. Is this a Mac OSX 10.6 / XCode 3.2.1 feature only? I’m running 10.5 and (corresponding latest) XCode 3.1.4.

  5. markj says:

    I’m not sure when this was added to Instruments, it might well be only for the latest version on Snow Leopard. Readon enough to upgrade – this and the static analyser too?

  6. Kim Le says:

    How’d you get the Xcode view you’re using around 40 seconds? That’s so cool with the build results window in the top left. Also, how’d you get the Debugger view in the “Page” toolbar icon in the upper left of your xcode window?

  7. markj says:

    Thats Xcodes All-In-One view. Close your Xcode project but keep the app open. Go to preferences..general. Change Layout. There are 3 alternatives, try them and see what you like.

  8. John B. says:

    Bless you, kind sir.

    I am weeping tears of joy and gratitude.

  9. RIck Windham says:

    Mark, very nice but I have one question. The NSZombieEnabled check box does not appear if I am debugging on the device. I keep reading it can be done but nothing I’ve tried works. Can you tell me how to enable NSZombie on the device? Thanks!

  10. markj says:

    Hmmm, I don’t know. But… for performance testing you need to test on the device. For memory debugging you don’t need to be on the device. In fact the simulator is much better because of the better workflow speed. Note… if you are having memory problems when the OS sends the low memory warning, you can trigger that yourself from the iPhone simulator apps menu.

  11. RIck Windham says:

    The problem only occurs on the device. If I set the NSZombieEnabled YES environment on the executable the debugger will give me the address the message was sent to, but I don’t get debugger console messages with Instruments running. (I could check the device console log I guess) Thanks it’s a great tutorial!

  12. Wendy says:

    I think this is a great tool, but I ran into two problems…
    I couldn’t enable Zombie Detection because it doesn’t work on a device. It only works with the simulator.
    My app doesn’t fully function in the simulator because the simulator doesn’t support mail, and I’m also using core data, which is harder to do in a simulator. Therefore, I really need to test on the device, not the simulator. Also, my memory error appears to be coming from taking pictures and sending mail which are not supported on the simulator.

    So, I tried this with the simulator anyway, and I got a message about something else… It pointed tok two lines of code that were commented out and it thought that they were each executed twice. I don’t know why I got that data. It seems very strange.

    I think this is a very powerful and tremendously useful tool, and I hope to see it working with a device at some point.
    I like the video, and especially the screenshots where you clearly describe the steps.
    Thanks.

  13. Jorge Bernal says:

    Cool! My guess was that Instruments would allow that easily, but I wasn’t expecting such a simple solution. Thanks for the video :)

  14. Conrad Kramer says:

    I have errors occurring in my process that is on a different thread. I use [NSThread detachNewThreadSelector:... toTarget:self withObject:nil] and I don’t get any calls from NSZombie, and in GDB it doesn’t say EXC_BAD_ACCESS. It just quits. Any help would be appreciated.

    Thanks,
    Conrad Kramer
    email me at: conradkramersapps@gmail.com

  15. Qrikko says:

    Hi, just wanted to get a confirmation about the Leopard / Snow Leopard thing. Is this only working when running Snow Leopard? I don’t get the option when I run in instrument, only have:
    Record Reference count
    Discard unrecorded data upon stop

    Would be nice to know if it’s a Snow Leopard thing or if I am just doing it wrong.

    Thanks.

  16. markj says:

    I think its Snow Leopard only.

  17. Xcode Lover says:

    You are a rock star! Thanks for this article and the video.

  18. Todd says:

    Very helpful – thanks!

  19. Pythic BV says:

    Awesome! I just spent two whole evenings tracking down the release of an object …

    I’m quite a xcode n00b but seems that lStr = [lStr stringByAppendingString:@","] is not keeping the lStr original settings like retain :(

    lStr = [lStr stringByAppendingString:@","] retain]; fixed my problem . Not sure if this is the correct way to fix it , but for now it works ..

    Thanks again !!

  20. Graham says:

    Well, after trying many different ways, and on two instances of “EXC_BAD_ACCESS” I do not get the “Zombie Messaged” popup that you get, I can set “Record reference counts” and “Enable NSZombie Detection” but mine just runs, the instrument starts recording, then just quits with recording stopped.

    Sigh. Any thoughts?

  21. ferenc says:

    nice one!
    why i had this simple bug ;)
    what should be done if the unnecessary release actually happens in [super dealloc] (super is a UIView)?

    well anyway thanks for the eyeopening video!!

  22. Z@K! says:

    Does this still work in instruments v.2.7 (2529)? I did not have a checkbox for “Enable NSZombie detection”…

    Thanks,
    Zak

  23. John says:

    Hi everyone

    I tried to following the instruction to use NSZombie.. It doesn’t seem to work on my Xcode.

    I am a brand new programming in Objective C. I even have a new environment variable NSZombieEnabled in arguments section for the Executables.
    The choice ‘Enable NSZombie’ Detection did not show up in my Instrument. I wonder what’s going on?

    i.e. I am not allowed to enable NSZombie detection?
    Any help will be greatly appreciated!

    John

Trackbacks

Check out what others are saying about this post...
  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 [...]



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

  • Categories

  • markjnet