Dectect iOS Zombies in UnitTests via Instruments

Ever get those pesky EXC_BAD_ACCESS errors due to over released memory in unit tests? For me, it was crashing octest, and other tests were not executing. Although, I could set a breakpoint in the debugger when this happened, but I wasn’t able to figure out where this memory was allocated and when it was released.

Here is how I found the root cause for my issue. I’d love to hear if there is an easier way.

  • Step 1. Ensure NSZombieEnabled=YES is set. Set up a breakpoint in setUp method of your unit test.
  • Step 2. Run the unit test in the simulator with a debugger (⌘U).
  • Step 3. After the breakpoint is hit. Launch Instruments and use the “Zombies” instrument.
  • Step 4. Select Target->Attach to Process and then select “octest”.
  • Step 5. Continue your code in Xcode, and watch as the zombie is detected and a flag is added in instruments.
  • Step 6. Double click on the flag to see where the memory was allocated and fix the issue.
  • Step 7. Repeat to ensure that the issue is fixed.