Tools/Debugging/Valgrind

=Valgrind=

Valgrind can be used to detect memory errors that a debugger such as gdb would not otherwise find.

Starting Blender
install valgrind on your system.

OS X
http://blog.loudhush.ro/2010/02/compiling-valgrind-on-snow-leopard.html

Linux
On ubuntu/debian

Now you can go to the place you built blender and type...

This will run blender in valgrind and generate errors that you can use to find bugs before they crash blender or errors that may cause unpredictable behavior.

These options give more useful output but make blender run slower too.

Analyzing Output
There are a few things to consider when reading the output,
 * un-initialized memory may be caused by external libraries, unless these libraries are compiled with debug symbols there wont be as useful info. This cases are easy enough to identify, its just good to be mindful that not all reports are caused by issues in blender.
 * Reports about un-initialized stack variables will show up at the function beginning even if the variable is defined within a block of the code.

False Positives
Valgrind tends to give errors with blender on startup which you can safely ignore.
 * X11: Anything related to X11/Xlib.
 * Anything relating to drivers (sound, video).
 * OpenGL: The OpenGL driver.
 * Python: Python uses its own allocator which valgrind detects errors in. (can be resolved with a custom Python build - see below)
 * SDL/Sound:
 * Blenders Undo System: Uses memory comparison which causes errors.

The kinds of errors you get also depend on how valgrind is installed, since some installations come with default suppression files.

As stated, these happen most when starting blender, so be prepared to ignore many errors on startup.

Compiler Options
Another source of false positives can stem from the options used when compiling blender. While not essential starting out. if you are spending a long time to track a bug down its better to be sure its reported correctly.
 * compile with debug enabled.
 * disable optimizations
 * disable in-lining

for gcc these flags are -O0 -g3 -fno-inline

Python Compiler Options
As mentioned before, python can be the cause of many false positives.

This can be avoided by building python without its own malloc replacement '--without-pymalloc' and will quiet most false positives.

Example of building python with debug options and without pymalloc.

CFLAGS="-fno-inline -g3 -O0" ./configure --prefix=/opt/py33 --enable-shared --with-pydebug --without-pymalloc --with-valgrind make make install

If you really don't need python, you can also disable the build option WITH_PYTHON but this will remove most of the user interface.

Debugging
This requires using gdbserver and isn't essential for valgrind to be useful.

see: http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver-gdb

Error suppression
''*Note*. setting suppressions is not strictly necessary but if you run valgrind a lot you may want to set it up - trying to find useful errors in thousands of reports is not really fun.''

If you try running the command above you'll see meny errors, most of which are not useful. to address this you need to make a suppressions file.

Generic Suppressions
Note: heavily based on http://stackoverflow.com/questions/2375726/how-do-you-tell-valgrind-to-completely-suppress-a-particular-so-file.

We said above that we could ignore Python, OpenGL driver, etc. errors. You can tell valgrind to silently do that by using its  option and a text file defining ignore rules.

The general format is basically:

Here is a file that suppress any error for a start & stop of Blender, on my machine. It should be easy to adapt (just check the lib paths, and replace the OpenGL driver part by your own version):. Simply use it like this:

Warning: This file suppresses thousands of errors! In case you are investigating an area it “silents” (Blender's GPU draw code, and mostly Python and fglrx), please consider commenting out related rules.

Refining
Above file (once adapted to your system) should cover nearly all false positives. However, you may want to add more specific rules. In this case, use the following method:

This will add suppression rules to  for all errors not already covered by your current   file. Open blender and do some operations generating the errors you want to get rid of…

Open, select the errors you want to ignore, and copy their content between the braces   into your   file. You should also add some comments about what/why you add those rules.

Now you can can use valgrind as usual (using the  option), just keep in mind you disabled a bunch of errors, in case you can’t find what you are looking for!

Some errors may keep appearing and you can ignore them:
 * Undo - blender compares un-initialized memory when running undo, you cant supress this from 1 place because undo is called in many areas.
 * Python - Python does its own memory management which confuses valgrind, and is also called in many places.