Friday, September 21, 2012


Starting with MonoTouch 5.4 it is possible to use Valgrind on a MonoTouch project. In some cases Valgrind is invaluable in finding strange crashes, in particular when freed memory is accessed.

This is what you need to do:

Download latest Valgrind (3.7.0 as of this writing). Extract somewhere, and compile:

./configure --enable-only32bit --prefix=/usr/local
sudo make install

Now open the project you want to valgrind, and set the VALGRIND environment variable to the command to execute to use valgrind:

Run (not debug) your project. You will see this in the Application Output:

In particular read the second line, you need to do exactly as it says: use gdb to attach to the app and then detach again. Once you've done this the app should start up (slowly, Valgrind makes your app a lot slower). Quite much output is printed to the Application Output, most of it is just noise though.

The messages you should be looking for (if you're trying to figure out why your app is crashing), are:

  • Invalid read of size X
  • Invalid write of size X

You can try to track down the reason for the following messages if you're just trying to find out why the app behaves strangely. Just have in mind that most of these messages are harmless, it can be due to "smart" optimizations in the code or by the compiler which valgrind doesn't understand.

  • Conditional jump or move depends on uninitialised value(s)
  • Use of uninitialised value of size X

For a thorough explanation of all these messages from Valgrind you should read their documentation.