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.
- Open your project or workspace in Xcode.
- Go to
Product > Scheme > Edit Scheme...(or pressCmd + <). - In the left sidebar, select
Rununder your app’s scheme. Make sureInfoshowsBuild Configuration: Debug. - Open the
Diagnosticstab at the top. - Under
Memory Management, checkEnable Zombie Objects. - Click
Closeand run your app withProduct > 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.
- Go to
Product > Scheme > Edit Scheme.... - Select
Runin the left panel and open theArgumentstab. - Under
Environment Variables, click+. - Add
Name: NSZombieEnabledwithValue: YES. - Click
Closeand 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
- Run with zombies enabled and trigger the steps that previously caused
EXC_BAD_ACCESS. - When the zombie is messaged, Xcode stops at the call site. Open the Debug Navigator and the console.
- 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. - Use
Debug > Debug Workflow > View Memoryor the Memory Graph button (Debug Memory Graph) to see what still references that address. Check forweakvsunownedcaptures 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)
}
- Fix the ownership. Make delegates
weak, captureselfweakly in escaping closures, and avoidUnmanaged.passUnretainedunless 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 Objectsoff by default. Enable it only for a local Debug run when investigatingEXC_BAD_ACCESS. - Never enable zombies in a Release build, Archive, or TestFlight build. It prevents deallocation and leaks all objects.
- Prefer
weakoverunownedandunsafe_unretainedfor delegates and closure captures. Useunownedonly when you can prove the lifetime. - Avoid
Unmanagedand__unsafe_unretainedbridging unless you manually retain. When you must bridge Core Foundation, use__bridge_transfer/__bridge_retainedcorrectly. - Turn on
Malloc Stack Loggingor 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.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.