Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get in script whether valgrind found memory leaks?

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?

like image 586
BЈовић Avatar asked Oct 08 '13 11:10

BЈовић


People also ask

Does Valgrind detect memory leaks?

Valgrind Memcheck is a tool that detects memory leaks and memory errors. Some of the most difficult C bugs come from mismanagement of memory: allocating the wrong size, using an uninitialized pointer, accessing memory after it was freed, overrunning a buffer, and so on.

Where can I find Valgrind memory leaks?

To get the details you need, add --show-leak-kinds=reachable or --show-leak-kinds=all to the Valgrind command line (together with --leak-check=full ). Now you will also get backtraces showing where still reachable memory blocks were allocated in your program.

How do you use a Valgrind leak check?

To run Valgrind, pass the executable as an argument (along with any parameters to the program). The flags are, in short: --leak-check=full : "each individual leak will be shown in detail" --show-leak-kinds=all : Show all of "definite, indirect, possible, reachable" leak kinds in the "full" report.

How can I see Valgrind errors?

Look for function names and line numbers If you compile your program with the -g flag, Valgrind will show you the function names and line numbers where errors occur.


1 Answers

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.

like image 112
devnull Avatar answered Sep 22 '22 06:09

devnull