Are there any invalid write (memory corruption) detection tools for iOS Xcode development?
ios, memory-corruption, xcode
Solution
Use Guard Malloc, which is supplied with Xcode.
Click on your application name, to the right of the top button to bring up the list of schemes in your project. Select 'Edit Scheme...' at the bottom. In the dialogue that comes up select the 'Diagnostics' tab. In there, tick to enable Guard Malloc.
Apple's documentation on exactly what it and what some of the other options there do is here but the text logged to the console when you run pretty much explains it all:
Allocations will be placed on 16 byte boundaries.
- Some buffer overruns may not be noticed.
- Applications using vector instructions (e.g., SSE) should work.
Each new allocation will start on a new page. Pages will be boxed out with appropriate MMU flags set so that out-of-bounds accesses raise EXC_BAD_ACCESS. So it'll catch 90% of memory access errors at the moment the access occurs, at the cost of your program running significantly more slowly due to cache inefficiency.
Cynically crafting examples, this code:
char *array = (char *)malloc(17);
array[31] = 9;
... will probably not raise an exception with or without Guard Malloc. This code:
char *array = (char *)malloc(17);
array[32] = 9;
... will probably not raise an exception ordinarily. With Guard Malloc enabled it will raise an exception upon execution of the second line.
So it's not as thorough as Valgrind but it takes barely a second to switch on and will likely catch significant mistakes.
Problem
Back when I was programming in C++, my go-to choice when I discovered that memory corruption was occurring was to fire up Valgrind and see if it caught an invalid write somewhere along the line. Now that the code I'm writing is supposed to run on 32-bit iPhones, and my development environment is Xcode on OSX Mavericks, Valgrind doesn't seem to be an option anymore; even putting aside architecture mismatches, Googling reveals many reports of incompatibility between Valgrind and Mavericks, especially with 32-bit binaries. However, I can't find anything in Xcode Instruments that has similar functionality - memory leak detectors are all well and good, but there's nothing to find those dastardly memory corruptions. Does anyone know of a good tool for detecting invalid writes when developing/debugging an iOS app?