Enable Zombie Objects

Enabling Zombie Object is a great tool for tracking tricky memory problems with Objective-C objects.

NOTE:  Zombie can be used when debugging both in the simulator and on a device.

It is a debugging facility from the Foundation framework that when an object is deallocated, its isa
pointer is modified (from your [super dealloc]) to be that of a runtime generated “zombie” class.

In other words, it tells the app to never actually release memory when you call release. Instead, objects that get released have their types changed to _NSZombie. The result is that if something  tries to act on that object after it has been freed, the debugger will break to the line that caused the error, instead of potentially crashing somewhere completely different.

ClassA* instanceOfClassA = [ClassA alloc];
[instanceOfClassA release];
NSLog(@"Name of ClassA = %@", instanceOfClassA.getName);

In this example we doing a wrong task. Here we are trying to have a the name of a property for an object that already been released. Running this code absolutely tends to crash. Sometimes it will crash later or in a different area of code. This can make tracking down the line that caused the problem quite difficult.

NSZombie will help us debug the problem perfectly. If we enable zombie object, subsequently messages sent to the zombie object cause logged message and can be put to break when running on debugger.

You should see the following print out in the console window:
2013-03-21 12:34:01.939 TestZombie[64340:f803] *** -[ClassA getName]: message sent to deallocated instance 0x6c718e0

Zombie can be controlled by an environment variable called Zombie Objects. Since turning on Zombie Objects means the memory zone occupied by the object will never get freed, there will be huge drag on the memory allocation. So it is strongly recommended to leave it disabled in distribution configuration of your app.
Enabling Zombie object can help you track down much more complicated memory violations. It’s a great place to start if you’re seeing what you suspect is a memory stomp.

However, sometimes this won’t turn anything up. Perhaps your bug is being caused by a C/C++ class instance. If this is the case, then Enabling Zombie won’t be able to help you, because it tracks only Objective-C allocations and releases. If the problem has to do with an object that has been newed/deleted, then you need a different tool, Guard Malloc.