I wrote a C++ program that ran correctly over valgrind but I have notices something really strange:
==23369== HEAP SUMMARY:
==23369== in use at exit: 0 bytes in 0 blocks
==23369== total heap usage: 1 allocs, 1 frees, 72,704 bytes allocated
Where this allocation came from?
I only called sbrk()
and mmap()
but never used malloc
or new
so how can I know what is causing this?
Update: I removed #include <iostream>
and it worked! Still I didn't get why is this. I just included a library which I didn't use... (I have cassert and removing #include <iostream>
caused no problems
You may try to debug your program(We need to install the debug symbol for libstdc++ at first, see this):
gdb $YOUR_PROGRAM
b malloc
r
bt
On my PC with this nearly empty program:
#include <iostream>
int main(int argc, char* argv[]) { return 0; }
We get the call trace only once (Same as the OP, and the size 72,704 is exactly the same!):
#0 __GI___libc_malloc (bytes=bytes@entry=72704) at malloc.c:3043
#1 0x00007ffff7e2eb8a in (anonymous namespace)::pool::pool (
this=0x7ffff7fc3b40 <(anonymous namespace)::emergency_pool>)
at ../../../../src/libstdc++-v3/libsupc++/eh_alloc.cc:123
#2 __static_initialization_and_destruction_0 (__priority=65535, __initialize_p=1)
at ../../../../src/libstdc++-v3/libsupc++/eh_alloc.cc:262
#3 _GLOBAL__sub_I_eh_alloc.cc(void) ()
at ../../../../src/libstdc++-v3/libsupc++/eh_alloc.cc:338
#4 0x00007ffff7fdfdbe in ?? () from /lib64/ld-linux-x86-64.so.2
#5 0x00007ffff7fdfea8 in ?? () from /lib64/ld-linux-x86-64.so.2
#6 0x00007ffff7fcf10a in ?? () from /lib64/ld-linux-x86-64.so.2
If we remove the #include <iostream>
, we won't hit the malloc
again!
Now we confirmed that the malloc is called by libstdc++(The STL) library itself.
There is a global object emergency_pool
in STL, which will call malloc
during destructing to reserve memory, if we include iostream
it will be constructed before main
.