Debugging with NSZombieEnabled
NSZombies-whhat?!
NSZombieEnabled, sounds like a crappy horror movie a little; but in reality it’s a often overlooked environment variable that can make debugging applications a lot easier. If you’ve ever spent more than five minutes debugging a EXC_BAD_ACCESS crash in your Cocoa application, you should know about NSZombieEnabled, simple as that.
So what does it do?
NSZombiesEnabled will instruct the foundation code to never trully dealloc any objects, instead they are changed to _NSZombie objects and not mark the memory holding the object as free. Now whenever a call is made to a _NSZombie object instead of getting a EXC_BAD_ACCESS message an informative log will be outputted allowing you to see the object which is being called. Good huh?
So how do I use it?
In XCode simply,
- Double-click your executable in the Executable group of your project.
- Click on the Arguments tab.
- In the “Variables to be set in the environment:” section, create a new variable called NSZombieEnabled and set it to YES.
You should ensure you turn NSZombieEnabled off when you are not actively using it as no objects in you app will every be freed, therefore you will consume a lot of memory. So in general only use NSZombiesEnabled when you get an EXC_BAD_ACCESS error and can’t instantly find the culprit.
Some people even go so far as to suggest adding some code to check for the variable and log a message just in case you didn’t realise you still had it on.
if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled")) {
NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");
}
Similar Posts
One Response to “Debugging with NSZombieEnabled”
Leave a Reply
















vish on September 24th, 2009
Thanks a lot dear.