Fix: Enable Zombie Objects to Debug Over-Released Objects in Xcode

Written by

in

Quick answer: Open your scheme editor (⌘⌥R), select the Diagnostics tab, and check “Enable Zombie Objects”. This turns deallocated instances into NSZombie objects that log a warning when messaged, revealing the exact over-release location.

Why this happens

When an Objective-C or Swift object is released one time too many, its memory is freed but a dangling pointer remains. The next message sent to that pointer triggers EXC_BAD_ACCESS or a cryptic message sent to deallocated instance crash. Because the object no longer exists, the stack trace rarely points to the actual over-release site.

Zombie objects solve this by never truly deallocating. Instead, the runtime swaps the freed instance for a special NSZombie subclass that traps every message and logs the class name, selector, and address to the console. The log tells you which object was over-released and where the bad message originated.

The feature is a debug-only aid; it increases memory usage because nothing is really freed, and Xcode automatically ignores the setting when you archive for App Store submission.

How to fix it

1. Enable Zombie Objects via the Scheme Diagnostics tab (recommended)

  1. Press ⌘⌥R (or choose Product ▸ Scheme ▸ Edit Scheme…).
  2. Select the Run action on the left, then click the Diagnostics tab.
  3. Check Enable Zombie Objects.
  4. Close the sheet and run the app (⌘R).

When the over-release occurs, the console prints a line such as:

*** -[MyViewController release]: message sent to deallocated instance 0x600003a8c0

The address lets you set a symbolic breakpoint on -[NSObject release] or use the Debug ▸ Breakpoints ▸ Create Symbolic Breakpoint dialog to pause exactly at the offending release.

2. Alternative: Add the environment variable manually

If you prefer the classic method or need to script it:

  1. In the same Edit Scheme sheet, choose the Arguments tab.
  2. Under Environment Variables, click + and add:
    – Name: NSZombieEnabled
    – Value: YES
  3. Optionally add CFZombieLevel with value 5 for Core Foundation zombies.

This achieves the same result; the Diagnostics checkbox simply writes these variables for you.

3. Verify it works

Trigger the crash scenario. You should see a zombie warning in the console before the process dies. If the app still crashes silently, ensure you are running the Debug configuration (the default for Run) and that Strip Debug Symbols is No in Build Settings.

How to prevent it

  • Adopt ARC (Automatic Reference Counting) for all new Objective-C code; it eliminates manual retain/release errors.
  • Use Swift wherever possible—its ownership model prevents over-release by design.
  • Run the Static Analyzer (⌘⇧B) regularly; it catches many retain/release mismatches at compile time.
  • Enable Malloc Scribble and Guard Malloc in the same Diagnostics tab for additional heap-corruption detection.
  • Write unit tests that exercise object lifecycles under stress (background threads, low-memory simulations).

FAQ

Does enabling zombies affect App Store builds?

No. Xcode ignores the Enable Zombie Objects setting when you choose Product ▸ Archive, so your release binary is unaffected.

Can I use zombies with Swift objects?

Yes. Swift classes that inherit from NSObject (or are @objc) participate in the same reference-counting runtime, so over-releases of those instances are caught. Pure Swift structs and classes without Objective-C ancestry use a different memory model and cannot become zombies.

Why do I still get EXC_BAD_ACCESS with zombies on?

Zombies only catch messages sent to already deallocated Objective-C objects. They do not detect buffer overruns, use-after-free of C/C++ allocations, or stack corruption. Combine zombies with Address Sanitizer (-fsanitize=address) for broader coverage.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.