Skip to main content

Xcode 7: Address Sanitizer

·2 mins

Xcode 7 adds Address Sanitizer. Address Sanitizer helps find memory corruption problems, such as buffer overflows and using freed memory.

Keep in mind that Address Sanitizer works only with C-based languages: C, C++, and Objective-C. If you are coding in Swift, Address Sanitizer will not help you. Also remember that your application runs slower in Address Sanitizer.

Turning on Address Sanitizer #

Address Sanitizer is turned off initially. Use the scheme editor to turn on Address Sanitizer. Open the scheme editor and select the Run step. Click the Diagnostics button at the top of the scheme editor. Select the Enable Address Sanitizer checkbox.

SchemeAddressSanitizer

If the Enable Address Sanitizer checkbox is disabled, deselect the Enable Malloc Scribble, Enable Malloc Guard Edges, and Enable Guard Malloc checkboxes.

You can also use Address Sanitizer in your unit tests. Select the Test step, click the Diagnostics button, and select the Enable Address Sanitizer checkbox.

Rebuild your project to run it in Address Sanitizer.

Using Address Sanitizer #

After turning on Address Sanitizer and rebuilding your project, run your application. If your application does something to corrupt memory, Xcode will pause your application and display Address Sanitizer’s output in the debug console. It’s similar to what Xcode’s debugger does when your application raises an exception or crashes. I wish I could provide more information, but I use Swift so I can’t take advantage of Address Sanitizer in my code.

Additional Information #

To learn more about Address Sanitizer, watch the 2015 WWDC session Advanced Debugging and the Address Sanitizer.