Using Valgrind

Valgrind is an efficient way to debug memory leaks and invalid accesses in your ØMQ applications.

This page is a short tutorial and especially on how to create suppression files that allow Valgrind to report your errors, but not those in libzmq (which are deliberate and performance-related).

Here is how to run your application (a linked main program) under the Valgrind memory checker:

valgrind --tool=memcheck --leak-check=full yourapp

You'll see access to uninitialized memory as your app runs, and you'll see memory leaks when you exit it, using Ctrl-C (if you properly catch and handle this).

By default you'll also see lots of errors caused by the libzmq calls like socketcall.sendto(msg). You can reconfigure and rebuild libzmq using the environment setting CPPFLAGS=-DZMQ_MAKE_VALGRIND_HAPPY but this is tedious. What works better are to use suppression files.

This is the best known suppression file, taken from the CZMQ project (copy/paste as valgrind.supp):

{
   <socketcall_sendto>
   Memcheck:Param
   socketcall.sendto(msg)
   fun:send
   ...
}
{
   <socketcall_sendto>
   Memcheck:Param
   socketcall.send(msg)
   fun:send
   ...
}

To use the suppression file valgrind.supp, add the --suppressions option:

valgrind --tool=memcheck --leak-check=full --suppressions=./valgrind.supp yourapp

If you find new errors in libzmq, you can add them to the suppression file, and send your new version to the zeromq-dev list, or attach it to this page. This HowTo explains suppression files in more detail, but the short story is to run valgrind again with the --gen-suppressions=all and --log-file=yourapp.log options, and manually copy/paste the logged suppressions into the suppression file. You'll find the same error repeated over and over, so take the first instance, replace all 'fun:' lines that are outside libzmq with one line ..., and give a name to the new suppression.

You'll then repeat the process until you get no more libzmq errors.

Questions and discussion on the zeromq-dev list!