How to make valgrind report an error when there are still reachable allocs

valgrind still reachable
valgrind error summary
valgrind still reachable fix
how to use valgrind
valgrind options
valgrind definitely lost
possibly lost valgrind
valgrind indirectly lost

I'm writing a compiler that produces C code. The programs produced consist only of the main function, and they use a lot of memory, that is allocated with malloc(). Most of the memory allocated is used only in a small part of the program, and I thought it would be a good idea to free() it after use, since it's not going to be used again. I would be glad, then, if valgrind would report to me about memory not free()d in the end of the program, that is, still reachable memory. I'm using valgrind with --error-exitcode=1 inside a Makefile, to check for this kind of problem automatically.

The question is: is there a way to make valgrind exit with 1 in case there are still reachable allocs?

An alternative to grepping through Valgrind output: modify your compiler so it emits:

int main() { return foo_main(); }
int foo_main() {  /* whatever you've emitted before */ }

Assuming you are not assigning allocated blocks to global variables (which would make no sense since you only have one function), you've just transformed "still reachable" into "definitely leaked".

Possibly even better transformation: don't call exit(0) in your main; change it to return 0; instead. The net effect should be same as above -- __libc_main will now call exit for you, and all local variables in main will be out of scope by that time.

Memcheck: a memory error detector, "Still reachable" blocks are very common and arguably not a problem. So, by default They have been tested with some gcc versions (e.g. 4.4 and 4.7). Controls whether Memcheck reports uses of undefined value errors. With alloc- then-free , a stack trace is recorded at allocation time, and is associated with the block. possibly lost: heap-allocated memory that was never freed to which valgrind cannot be sure whether there is a pointer or not. still reachable: heap-allocated memory that was never freed to which the program still has a pointer at exit. These categorizations indicate whether the program has retained a pointer to the memory at exit.

The valgrind manual says:

Indirectly lost and still reachable blocks are not counted as true "errors", even if --show-reachable=yes is specified and they are printed; this is because such blocks don't need direct fixing by the programmer.

I have found no way to make valgrind report "still reachable"s as error. It seems to be that your only option to do this (other than patching valgrind) is to capture the output of valgrind and parse the "still reachable" line.

Valgrind, When building Valgrind, 'make' dies partway with an assertion failure, Valgrind reports 'still reachable' memory leaks involving these classes at the exit of the Memcheck's uninitialised value errors are hard to track down, because they are� This program has a memory error, resulting in the value of x being 10 at the end rather than 0. However, valgrind will not report any errors. A more serious limitation that you will encounter is that valgrind checks programs dynamically-- that is, it checks during actual program execution whether any leaks actually occurred for that execution

The poroper options to use to exit with error when there is a reachable block at exit:

valgrind --tool=memcheck --leak-check=full --show-reachable=yes --errors-for-leak-kinds=all

From Valgrind manual:

Because there are different kinds of leaks with different severities, an interesting question is: which leaks should be counted as true "errors" and which should not?

The answer to this question affects the numbers printed in the ERROR SUMMARY line, and also the effect of the --error-exitcode option. First, a leak is only counted as a true "error" if --leak-check=full is specified. Then, the option --errors-for-leak-kinds= controls the set of leak kinds to consider as errors. The default value is --errors-for-leak-kinds=definite,possible

CS107 Valgrind Memcheck, Valgrind reports two types of issues: memory errors and memory leaks. error can still seem to work correctly because you manage to get "lucky" much of the time. 0 bytes in 0 blocks ==32251== still reachable: 0 bytes in 0 blocks == 32251== It's pretty easy to tell when there's a leak: the alloc/free counts don't match up� The loss record numbers can be used in the Valgrind gdbserver to list the addresses of the leaked blocks and/or give more details about how a block is still reachable. The option --show-leak-kinds=<set> controls the set of leak kinds to show when --leak-check=full is specified.

Alternatively you can have a small shell script in your makefile to grep through output logs of valgrind and exit accordingly.

CS107 Guide to valgrind, Valgrind reports on both with equal vigor, but please don't you conflate errors with a memory error can still seem to work correctly because you manage to get "lucky" much of the time. ==5942== still reachable: 0 bytes in 0 blocks. It's pretty easy to tell when there's a leak: the alloc/free counts don't match up and you� Questions: Take the following trivial program: #include <iostream> int main() { return 0; } If I run this using valgrind, I’m told that there are 72,704 bytes in 1 blocks that are still reachable. There have been extensive discussions on SO about whether or not to worry about still reachable warnings–I’m not concerned about that.

[PDF] Understanding Valgrind memory leak reports, Valgrind may also report Invalid Read/Write warnings on variables in the stack, free() / delete / delete[]' error. A block of memory is reported to be Still Reachable when Memcheck finds, A leak is considered Definitely Lost when, at process exit, there is no pointer total heap usage : 1 allocs , 0 frees , 4 bytes allocated. 1.4.1 Still reachable blocks A block of memory is reported to be Still Reachable when Memcheck finds, after process execution ends, at least one pointer with the start address of the block (a start-pointer). This pointer found can be either in stack (a global pointer, or a pointer in the main function),

An Introduction to Valgrind Memcheck, Here Valgrind is telling us it thinks there are still 1,552 bytes from two As the output says, we should run with --leak-check=full to get a full error report. in use at exit: 1,552 bytes in 2 blocks ==22679== total heap usage: 2 allocs, Valgrind tells us that the 552 still reachable bytes were allocated from line� Valgrind does not demangle function names when searching for applicable suppressions, because to do otherwise would make suppression file contents dependent on the state of Valgrind's demangling machinery, and also slow down suppression matching.

CS 225, total heap usage: 65,665 allocs, 65,663 frees, 2,132,319,481 bytes allocated If Valgrind reports that you have memory leaks, you can run: possibly lost: 20,572,032 bytes in 1 blocks ==27013== still reachable: 0 bytes in 0 blocks There are many possible errors Valgrind can report, and details on all can be found in� Valgrind for ns-3 is known to work on recent Linux systems that do not have gtk enabled. In particular, there seems to be a leak in some libraries related to gtk, which is used in ns-3 for the GTK Config Store component. It may be possible to add some suppressions, but it is likely easier to try to disable gtk when you want to use valgrind.