Friday, September 21, 2012

Valgrind

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
make
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.

2 comments:

  1. Hi Rolf! Is the Valgrind compatibility still supported under Xamarin Studio? Valgrind prints some bug warning/error messages after a few seconds of execution. I use Valgrind 3.8.1. Valgrind has a bug when it tries to call syscall 369 (prlimit 64, for 64-bits). Here is the exact message I keep seeing:

    --66783-- WARNING: unhandled syscall: unix:369
    --66783-- You may be able to write your own handler.
    --66783-- Read the file README_MISSING_SYSCALL_OR_IOCTL.
    --66783-- Nevertheless we consider this a bug. Please report
    --66783-- it at http://valgrind.org/support/bug_reports.html.
    --66783-- WARNING: unhandled syscall: unix:369

    Thanks, Paul

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete