NSZombies and the Program received signal: “EXC_BAD_ACCESS” exception

[The following article applies to Xcode 3.x]

Even when following the memory management techniques explained by Steve "Scotty" Scott I sometimes run across my most dreaded runtime error;

Program received signal: “EXC_BAD_ACCESS”

This happens when you try to access an object that has already been released. Sadly the error message doesn't give you any information on what object you were trying to access, but luckily you can call up some zombies to help out.

Control-click the Executable you are trying to debug and select Get Info

Use the + button to add a new environment variable called NSZombieEnabled and set its value to YES.

When you run your app with this variable enabled, all deallocated objects are kept in memory and you will get a better error message in the console once one of them receives the unexpected call which crashes your app.

The debugger stack trace can at this point help you figure out exactly where in you code the problem occurred. Hovering the mouse cursor over the offending object will indicate that it is indeed an NSZombie.

Now that you know which object was accessed after being released you should be able to squash the bug in a few minutes by making sure to apply the memory management techniques correctly on it.

Make sure to uncheck the NSZombieEnabled variable before you ship your code, otherwise no memory is ever freed from you app. If you forget to uncheck it all released objects will be kept around in a zombie state, consuming your memory and eventually getting your app force killed by the system.

Since it is important and you need to remember this, say the following phrase out loud;

"I will not ship zombie infested code to my customers."