How to Enable NSZombie to Debug EXC_BAD_ACCESS in Xcode

Written by

in

Quick answer: In Xcode, go to Product > Scheme > Edit Scheme > Run > Diagnostics and check Enable Zombie Objects. Re-run your app and the console will log the exact deallocated object receiving a message, e.g. message sent to deallocated instance.

Crashes with EXC_BAD_ACCESS and no useful stack trace are almost always a message sent to an object that has already been deallocated. Enabling Zombie Objects (the NSZombieEnabled flag) stops the runtime from freeing objects and instead turns them into zombies that log exactly which class and address was accessed after deallocation.

Why this happens

When an object is deallocated, its memory is marked as free. If another part of your code still holds a dangling pointer and tries to call a method on that memory, you get EXC_BAD_ACCESS (code=1, address=0x...) or EXC_BAD_ACCESS (code=2, ...). The debugger often stops on main or in assembly with no hint about which object was involved.

With Automatic Reference Counting (ARC), the compiler handles most retains and releases, but zombies still happen. Common causes are unowned or unsafe_unretained references, __unsafe_unretained bridged Core Foundation objects, manual Unmanaged handling, blocks that capture a deallocated object, or an over-release in Objective-C code mixed into the project. Delegates declared as assign instead of weak are a classic example.

Normally the deallocated memory is reused quickly, so the crash looks random. When Zombie Objects is enabled, the Objective-C runtime does not deallocate instances. Instead it replaces them with an _NSZombie placeholder that intercepts any message and reports *** -[ClassName method]: message sent to deallocated instance 0x.... That message tells you exactly what to look for.

How to fix it

Use the scheme setting to turn zombies on for the Debug Run action only. Never ship this to TestFlight or the App Store.

1. Enable Zombie Objects in the Scheme Diagnostics (recommended)

This is the modern equivalent of setting NSZombieEnabled and works on both Simulator and a physical device.

  1. Open your project or workspace in Xcode.
  2. Go to Product > Scheme > Edit Scheme... (or press Cmd + <).
  3. In the left sidebar, select Run under your app’s scheme. Make sure Info shows Build Configuration: Debug.
  4. Open the Diagnostics tab at the top.
  5. Under Memory Management, check Enable Zombie Objects.
  6. Click Close and run your app with Product > Run (Cmd + R).

Reproduce the crash. Instead of a bare EXC_BAD_ACCESS, Xcode will stop and the debug console will show something like:

*** -[MyViewController handleTap:]: message sent to deallocated instance 0x600001a8c9e0

The hex address lets you correlate with the object in the memory graph. The class name tells you which object was over-released. From there, check where that instance is created, retained, and released.

To disable zombies again, return to the same Diagnostics tab and uncheck the box. Zombies keep every deallocated object alive, so memory grows quickly and the app will eventually be terminated if you leave it on.

2. Enable NSZombieEnabled as an Environment Variable (legacy method)

Before the Diagnostics checkbox existed in Xcode 4.1, zombies were enabled via an environment variable. It does the same thing and is still valid.

  1. Go to Product > Scheme > Edit Scheme....
  2. Select Run in the left panel and open the Arguments tab.
  3. Under Environment Variables, click +.
  4. Add Name: NSZombieEnabled with Value: YES.
  5. Click Close and run.

You can verify it is active in the console log on launch. If you use both methods, either one is enough — the Diagnostics checkbox simply sets NSZombieEnabled for you. Remove the variable when you are done debugging.

3. Reproduce the crash and read the zombie message correctly

  1. Run with zombies enabled and trigger the steps that previously caused EXC_BAD_ACCESS.
  2. When the zombie is messaged, Xcode stops at the call site. Open the Debug Navigator and the console.
  3. Note the full message: class, selector, and address. For example, *** -[UIImageView setImage:]: message sent to deallocated instance 0x... points to an image view that was released while still in use.
  4. Use Debug > Debug Workflow > View Memory or the Memory Graph button (Debug Memory Graph) to see what still references that address. Check for weak vs unowned captures in closures:
// Risky: unowned will crash if self is deallocated before the closure runs
networkCall.completion = { [unowned self] result in
    self.updateUI(with: result)
}

// Safer: weak + guard
networkCall.completion = { [weak self] result in
    guard let self else { return }
    self.updateUI(with: result)
}
  1. Fix the ownership. Make delegates weak, capture self weakly in escaping closures, and avoid Unmanaged.passUnretained unless you guarantee lifetime.

If the console only shows the address without a class name, enable Malloc Stack Logging in the same Diagnostics tab (Malloc Stack Logging > Live Allocations). Then you can use malloc_history or the Memory Graph to see the allocation and free stack for that address.

4. When zombies are not enough

Zombie Objects only helps for Objective-C and Swift objects that go through the Objective-C runtime. For pure Swift structs, buffer overflows, or use-after-free in C/C++ code, use Product > Scheme > Edit Scheme > Run > Diagnostics > Enable Address Sanitizer instead. Address Sanitizer instruments memory access and catches a wider class of errors, but it is slower and cannot be combined with zombies in the same run. Use one at a time.

You can also profile with Instruments: Product > Profile and choose the Zombies template. Instruments records zombie events on a timeline, which is useful if the crash is hard to reproduce interactively.

How to prevent it

  • Leave Enable Zombie Objects off by default. Enable it only for a local Debug run when investigating EXC_BAD_ACCESS.
  • Never enable zombies in a Release build, Archive, or TestFlight build. It prevents deallocation and leaks all objects.
  • Prefer weak over unowned and unsafe_unretained for delegates and closure captures. Use unowned only when you can prove the lifetime.
  • Avoid Unmanaged and __unsafe_unretained bridging unless you manually retain. When you must bridge Core Foundation, use __bridge_transfer / __bridge_retained correctly.
  • Turn on Malloc Stack Logging or Address Sanitizer temporarily to get allocation backtraces for dangling pointers.
  • Add the Memory Graph Debugger to your workflow after fixing the crash to check for retain cycles and leaked zombies.

FAQ

Does Enable Zombie Objects work on a real device or only the Simulator?

It works on both. The setting is a runtime flag in your app’s Run scheme, not a Simulator-only feature. You can debug zombie crashes on a connected iPhone or iPad the same way as on the Simulator.

Should I leave NSZombieEnabled on for TestFlight or App Store builds?

No. Zombies never deallocate objects, so memory usage grows without bound and performance degrades. The flag is for local debugging only. Make sure the Diagnostics checkbox is unchecked and the NSZombieEnabled environment variable is removed before you Archive.

What is the difference between Zombie Objects and Address Sanitizer?

Zombie Objects replaces deallocated Objective-C objects with a placeholder that reports the class and selector of any later message. Address Sanitizer instruments all memory accesses and detects use-after-free, buffer overflows, and other memory errors in Swift, Objective-C, and C/C++. Use zombies for message sent to deallocated instance crashes; use Address Sanitizer for broader memory corruption.

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.