I have following code example. Class foo has a private member bar, which is a static string pointer. In the main function I am doing something completely not relevant with class foo.
In the end, valgrind gave me: still reachable: 32 bytes in 1 blocks
I have no idea how to handle that. Can you give me a hint?
In the reality, class foo is given in another file of a university project. I coded my assignment without any connection with class foo (until valgrind told me memory leak happend) Do I need to change the file containing class foo, which is not part of my assignment?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <string>
class foo {
foo() = default;
~foo() = default;
private:
static std::string* bar;
};
std::string* foo::bar = new std::string("0");
int main () {
// do some work without foo
return 0;
}
| |
Details:
Compile with:
$ g++ test.cc -o test
Valgrind command and output:
$ valgrind --leak-check=full --show-reachable=yes --show-leak-kinds=all ./test
==2424== Memcheck, a memory error detector
==2424== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2424== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2424== Command: ./test
==2424==
==2424==
==2424== HEAP SUMMARY:
==2424== in use at exit: 32 bytes in 1 blocks
==2424== total heap usage: 2 allocs, 1 frees, 72,736 bytes allocated
==2424==
==2424== 32 bytes in 1 blocks are still reachable in loss record 1 of 1
==2424== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2424== by 0x1089ED: __static_initialization_and_destruction_0(int, int) (in /tmp/test)
==2424== by 0x108A76: _GLOBAL__sub_I__ZN3foo3barB5cxx11E (in /tmp/test)
==2424== by 0x108ACC: __libc_csu_init (in /tmp/test)
==2424== by 0x53FEB27: (below main) (libc-start.c:266)
==2424==
==2424== LEAK SUMMARY:
==2424== definitely lost: 0 bytes in 0 blocks
==2424== indirectly lost: 0 bytes in 0 blocks
==2424== possibly lost: 0 bytes in 0 blocks
==2424== still reachable: 32 bytes in 1 blocks
==2424== suppressed: 0 bytes in 0 blocks
==2424==
==2424== For counts of detected and suppressed errors, rerun with: -v
==2424== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)