Zombies, Helping you not Send Messages to Deallocated Objects

Published January 19, 2013

I've just discovered that Zombies have an amazing innate ability to tell when you're messaging deallocated objects.

To see for yourself, enable Zombie Objects in XCode:

[Product] -> [Edit Scheme] -> [Run] -> [Diagnostics] -> [Objective-C] -> [Enable Zombie Objects]

From then on an NSZombie Object will be put in place of deallocated (reference count = 0) objects, so if you send a message to something that doesn't exist anymore, you can find it really easily when the time comes.

So if you do something like this...

NSString* zombieString =
    [[NSString alloc] initWithUTF8String:"Grr, brains! Argh"];

[zombieString dealloc];
[zombieString substringFromIndex:1];

You'll get a nice console message like this:

2013-01-18 23:22:32.780 Zombie Test[9544:c07] *** -[__NSCFString substringFromIndex:]: message sent to deallocated instance 0x745dbf0

BRAINS!