Sam
Sam

Reputation: 145

Fixing A bug for Valgrind for false memory leaks

I just as of yesterday installed Ubuntu(terminal) on my windows computer. In addition, when ahead and installed Valgrind, "sudo apt install Valgrind", to test everything out I went ahead and create a c++ hello world program. Then tested it with Valgrind. The Valgrind said I had about "72,704 bytes in 1 block". Further research, I performed suggested this was a bug of Valgrind with the c++ standard library functions, possible the iostream. My question is how do I go about fixing this bug. I can't ignore because if I am working on programs I need to able to accurately gauge where it's coming from. If anyone can provide a step by step solution for my problem in layman's term for the problem it would be invaluable. here is my code and errors I get:

#include <iostream>
using std::cout; using std::endl;

int main() {
        cout << "Hello World" << endl;
}

==195== Memcheck, a memory error detector
==195== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==195== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==195== Command: ./helloworld
==195==
Hello World
==195==
==195== HEAP SUMMARY:
==195==     in use at exit: 72,704 bytes in 1 blocks
==195==   total heap usage: 2 allocs, 1 frees, 76,800 bytes allocated
==195==
==195== LEAK SUMMARY:
==195==    definitely lost: 0 bytes in 0 blocks
==195==    indirectly lost: 0 bytes in 0 blocks
==195==      possibly lost: 0 bytes in 0 blocks
==195==    still reachable: 72,704 bytes in 1 blocks
==195==         suppressed: 0 bytes in 0 blocks
==195== Rerun with --leak-check=full to see details of leaked memory
==195==
==195== For counts of detected and suppressed errors, rerun with: -v
==195== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

update : Seems like I can sort deal with the false positive leaks by creating suppression files. However, I am not as tech savvy compared some people on here to know how to do that. How would I create suppression files on Valgrind (specifically for my situation)? Please explain it in LAYMAN's terms and please be as detailed as possible. second update: I have been semi-successful in suppressing the memory leaks but I would like to know if there is a more viable long term solution out there.

Upvotes: 2

Views: 1286

Answers (1)

Paul Floyd
Paul Floyd

Reputation: 6906

I would say that these are most likely genuine issues, but probably too minor for the libc/libstdc++ developers to fix.

You can generate suppressions in the Valgrind output by specifying --gen-suppressions=yes. This will generate output like this:

==28328== 56 bytes in 1 blocks are still reachable in loss record 1 of 7
==28328==    at 0x4C290F1: malloc (vg_replace_malloc.c:298)
==28328==    by 0x4111D8: xmalloc (xmalloc.c:43)
==28328==    by 0x41120B: xmemdup (xmalloc.c:115)
==28328==    by 0x40F899: clone_quoting_options (quotearg.c:102)
==28328==    by 0x40742A: decode_switches (ls.c:1957)
==28328==    by 0x40742A: main (ls.c:1280)
==28328== 
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc
   fun:xmalloc
   fun:xmemdup
   fun:clone_quoting_options
   fun:decode_switches
   fun:main
}

In the above, the section with the pid (==28328==) is the usual output. The section after it (delimited by braces) is the generated suppression. You can copy this block into a file, for instance my_suppressions, and then you can run valgrind and tell it to read the file --suppressions=my_suppressions.

If you are planning on maintaining your suppressions file for a long time, then it is best to put some meaningful (and unique) text in the place of 'insert_a_suppression_name_here'. This will help you monitor which suppressions are being used, since if you run valgrind in verbose mode (-v or --verbose) it will list all of the used suppressions. For instance

--30822-- used_suppression:      2 Example for SO my_suppressions:2 suppressed: 112 bytes in 2 blocks
--30822-- used_suppression:      4 U1004-ARM-_dl_relocate_object /remote/us01home48/pfloyd/tools/vg313/lib/valgrind/default.supp:1431

Upvotes: 1

Related Questions