More about zombies and XCode

Some time ago I did a very short introduction about enabling zombies when debugging in XCode. As a refresher, zombies is a Cocoa feature that turns any object going to be deallocated into a NSZombie instance. This will not prevent your application from crashing, but instead of getting a stack dump from Xcode that is a pain to read and understand, you get something more human–readable.

Working on a kernel extension for my Agilent DSO-X 2002A scope (see here), I frequently got EXC_BAD_ACCESS. One thing that nags me about these is that they are extremely difficult to debug

A pointer to an object is never set to nil when the object is released. The pointer continues to point to the same memory location that it always did. That doesn’t mean that whatever is now at that location is a valid object, however. For that reason, you should never use a pointer after you’ve released the object it points to. If you’re going to continue to use that pointer, you should change its value so that it’s either nil or points to some other (valid) object.

NSZombieEnabled is just a debugging tool that helps you find places in your code where you’re accessing invalid objects. If you’ve found a place where you’re doing that, you’ve found a bug in your code and you need to fix it, not tolerate it.

If the environment variable NSZombieEnabled is set to true, the compiler is directed to substitute an object of type NSZombie for any objects that are released to a reference count of zero. As a consequence, no more of those objects should exist. If a message is then sent to one of these deallocated objects (which are now NSZombie objects), the zombie is flagged, the app crashes, recording stops, and a Zombie Messaged dialog appears. You can then examine the retain and release history of the zombie object to determine exactly where the problem occurred.

Cool stuff. See more here.

Filed in Blog, Debugging and tagged .

Leave a Reply

Your email address will not be published. Required fields are marked *