Last night an iPhone Zombie (NSZombieEnabled) saved my life!
When developing for the iPhone / iPod Touch it is the developers responsibility to manage memory, not quite manually (never send a dealloc message to an object) but in a much lower level way than in higher level garbage collected languages.
Anyway, the application we are currently developing was crashing on the iPhone with a EXC_BAD_ACCESS error.
According to Apple an EXC_BAD_ACCESS is most likely to occur as a result of over-releasing or premature release of an object. Because the failure tends to occur sometime after the mistake is made the stack trace (if it’s even showing any of your code) is mostly useless. The debugger is equally useless as it has no way of identifying the dealloced object.
Step in NSZombieEnabled. With NSZombieEnabled you can compile your application so that it technically never frees any memory (ensure you remove this before distribution, obviously). Each time a dealloc message is sent to an object it’s Class is dynamically changed to _NSZombie and by default the allocated memory is never marked as free. This is useful because now when the erroneous message is sent to the already (incorrectly) dealloced object the debugger can report something sensible to the console. If you run this code inside the debugger, your application will stop when the EXC_BAD_ACCESS occurs and with a backtrace you can easily find the line of code which caused it.
Cool eh!
To enable it (in xcode)
1. Double-click an executable in the Executables group of your Xcode project.
2. Click the Arguments tab.
3. In the “Variables to be set in the environment:” section, make a variable called “NSZombieEnabled” and set its value to “YES”.
So, if you’re scratching your head with an EXC_BAD_ACCESS error hopefully you’ll find this helpful.
2 Comments to “Last night an iPhone Zombie (NSZombieEnabled) saved my life!”
Leave a Reply














[...] This post was Twitted by Sebollo – Real-url.org [...]
Newbie question.
How exactly do I ‘backtrace’ this? As far as I can tell the bogus ‘release’ happens somewhere else in my code. It’s not sequential.
Other tutorials mention running a malloc_history, but I since they are speaking about debugging a mac app. From what i can tell my malloc history isn’t on the iphone anymore. It’s been saved out to an index file on the mac.
Can you elaborate?
Rob