Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled

Thu, Apr 8

It’s only a matter of time before you’ll find yourself face to face with the dreaded EXC_BAD_ACCESS error message. This message generally occurs when you attempt to access an object that has previously been released.

NSZombieEnabled is an environment variable which can be helpful to track down the elusive object causing the problem. With the environment variable set, Cocoa will redirect all objects isa pointer to an NZZombie object once an objects retain count has reached zero. In the future, should you try and send a message to a previously released object, an exception will be raised showing a message similar to the following:

Setting NSZombieEnabled Environment Variable

In the Groups and Files section, expand the Executables section and right click on your app name and choose Get Info from the dialog:

Select the Arguments tab on the top and then add a new entry in the Variables to be set in the environment section. Name the new variable to NSZombieEnabled and set its value to YES.

Tracking Down the Error

Using a zombie, messages sent to an object that has already released will now generate the exception shown above, and following the stack track should now help you find the location of the object that is generating the EXC_BAD_ACCESS error.

Don’t forget to remove the environment variable, or at a minimum untick the checkbox once you find the source of the problem.

11 comments

Great help I was searching for it. Now it will be lot simpler to trackdown the run time error.

by Biranchi on Apr 8, 2010. #

Hi John,

Great tip – I’ve had this in my troubleshooting toolkit for several months now and for more than just BAD_ACCESS, everything and anything, especially those “hey the app just quit” what the self.heck?

by Greg Meach on Apr 8, 2010. #

Can you explain using the stack track to to find the location of the object? Are you somehow referencing the memory location and then going backwards from there? If that’s the case, how does it work?

by Justin on Apr 8, 2010. #

Hey Justin, the stack trace is shown in the upper left when you are viewing the debug screen. Depending on the nature of the error, often times you can walk backwards through the list (stack) and see where the problem originates.

John

by John Muchow on Apr 8, 2010. #

What a great tip, I need to check it right now. Should be default.
Thank you!

by iPhone developer on Apr 13, 2010. #

No, it’s not on by default because it’s dangerous. Zombie keeps all objects from being freed after release. It would screw things up if left on forever

by Justin on Apr 13, 2010. #

wow what a great tip for finding error. Thanks a lot

by Manoj on Apr 30, 2010. #

First class advice, you saved my life :-) Much appreciated, keep up the good work!

by JouniL on Oct 20, 2010. #

This has been the real life saver , thanks very much for the help.

by Lokesh Koli on Jan 15, 2012. #

Hi,

I tried this but it didnt give me any error at all but instead my app runs so smoothly when NSZombieenabled is set to yes.

I’ve been trying to figure out this exc bad access error for more than a month now but still no luck. =(

by Annie on Jan 26, 2012. #

I’ve found that the error is most often from trying to access an object that has been released.

by John Muchow on Jan 26, 2012. #