Enabling Address Sanitizer in Xcode to Catch Memory Errors
Learn how to turn on Address Sanitizer in an Xcode scheme to detect heap buffer overflows, use‑after‑free, and other memory errors during debugging.
23 Feb 2026, 04:33 UTC

Desired outcome
Build and run the app with Address Sanitizer (ASan) enabled so that any runtime memory error—such as a heap buffer overflow or use‑after‑free—produces a detailed report in the Xcode console.
Prerequisites
- Xcode 12 or later installed on the host machine.
- An iOS, macOS, or tvOS project open in Xcode.
- A reproducible code path that triggers a memory error (for example, writing past the end of a Swift array).
Procedure
- With the project open, choose
Product → Scheme → Edit Scheme…from the menu bar. - In the sheet that appears, select the Run action on the left.
- Click the Diagnostics tab at the top.
- Check the box labeled Enable Address Sanitizer. Optionally, you may also enable Enable Guard Malloc or Disable Stack Smashing Protection for extra checking.
- Close the sheet by clicking Close. Xcode will automatically use the edited scheme for subsequent builds.
- Build the project (⌘B) to ensure the new flags are applied and no linker errors appear.
Expected checks
After a successful build, launch the app (⌘R). When the faulty code executes, the Xcode console should display an ASan report that:
- Begins with a line of triple equals signs (
===). - States the error type (e.g.,
heap-buffer-overfloworuse-after-free). - Shows the offending memory address and a size.
- Includes a stack trace with the source file and line number highlighted.
Recovery options
If the build fails because of conflicting sanitizer flags, return to the Diagnostics tab and disable other sanitizers such as Thread Sanitizer or Undefined Behavior Sanitizer. If the app crashes immediately on launch, verify that the binary size increase (roughly 2×) is acceptable for a debug build; consider creating a dedicated debug configuration that keeps ASan enabled only for testing.
Limitations
- ASan roughly doubles the binary size and adds runtime overhead, which can affect timing‑sensitive code. Use it only in debug or test builds.
- ASan is not available for watchOS targets.
- Certain third‑party libraries that interpose on
malloc/freemay cause false positives; isolate the test to your own code when possible.
Practical verification
To confirm that ASan is working:
- Build the project with the edited scheme.
- Run the app and trigger the known memory error (e.g., press a button that executes an out‑of‑bounds write).
- Observe the Xcode console for a report starting with
===. - Check that the report mentions the expected error type and points to the source line you suspect.
- After verification, disable ASan in the Diagnostics tab to restore normal performance.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.