Use Address Sanitizer for suspected invalid memory access, Thread Sanitizer for data races, and Main Thread Checker for system API calls that require the main thread. For iOS, use Thread Sanitizer in Simulator.
Run a focused test
Open the scheme’s Diagnostics settings. Enable the relevant check and exercise the smallest failing scenario. After the fix, rerun the same scenario.
Turn a symptom into a diagnostic run
Write down the smallest action that reproduces the symptom. For a memory-access crash, start with Address Sanitizer. For inconsistent shared state accessed by several threads, try Thread Sanitizer. For a warning involving UIKit or AppKit calls from a background callback, check Main Thread Checker. Undefined Behavior Sanitizer is relevant to C-based code and does not cover Swift.
Open Product → Scheme → Edit Scheme and choose the Run or Test action used by your reproduction. Under Diagnostics enable the selected tool, then build and repeat the exact action. Use separate runs for Address Sanitizer and Thread Sanitizer. For an iOS race investigation with Thread Sanitizer, choose a supported Simulator destination.
For example, a mutable collection changes while a second thread reads it. Reproduce that interaction repeatedly and inspect the two accesses reported by Thread Sanitizer. Preserve the log and source locations, make a synchronization change that fits the code’s ownership model, then rerun the same case. Finish with the usual unsanitized tests too.
A clean diagnostic run only covers the paths that executed. Expand the reproduction to cancellation, repeated taps or background completion if those match the original report. Treat sanitizer timing as diagnostic behavior when comparing performance. The scenario is illustrative.
https://developer.apple.com/documentation/xcode/diagnosing-memory-thread-and-crash-issues-early