How to get in script whether valgrind found memory leaks?

BЈовић picture BЈовић · Oct 8, 2013 · Viewed 7.6k times · Source

I am running valgrind in a bash script, and directing the valgrind's output to a file. Like this :

valgrind --leak-check=full --show-reachable=yes --xml=yes --xml-file=unit_tests_valgrind.out.xml ./unit_tests_runner

The RET_VALUE=$? is going to put the return value of the program (unit_tests_runner above), not from valgrind. And the only way to check whether the valgrind found memory problems is to open the log.

Is there a way to check in a script whether there are memory problems? If yes, how?

Answer

devnull picture devnull · Oct 8, 2013

You seem to be looking for the --error-exitcode option.

Since it defaults to 0, the return code from Valgrind is the same as that of the process. Set it to a non-zero value instead.

From Valgrind core manual:

--error-exitcode=<number> [default: 0]

Specifies an alternative exit code to return if Valgrind reported any errors in the run. When set to the default value (zero), the return value from Valgrind will always be the return value of the process being simulated. When set to a nonzero value, that value is returned instead, if Valgrind detects any errors. This is useful for using Valgrind as part of an automated test suite, since it makes it easy to detect test cases for which Valgrind has reported errors, just by inspecting return codes.