Monday 1 October 2012

Tracking down zombies in objective-C applications

Aren't the worst bugs the ones that cause crashes infrequently seemingly randomly?

Even when you think you've found the problem and run the app successfully 100 times, you still can't be 100% sure you've got it! It's like trying to prove a negative.

Tracking down such a problem can be like the proverbial needle in a haystack. You can get clues from the crash report or debugger console, commenting out lines can work if the problem is fairly reproduceable but a good analysis tool can save many hours of poring over code and testing and re-testing.

In my experience a problem like this is most likely caused by object alloc / release. In such cases XCode will report 'EXC_BAD_ACCESS' which means (most likely but not always) that you're trying to access an object which has been released. 

Instruments with Zombies (XCode>Run with performance tool>Zombies) is a little baffling at first but is well worth it. If the bad access happens, it'll show you the life cycle of the object in question, where and when it was retained and released.

No comments:

Post a Comment