Memory Leak using STL - gcc-3.2 and gcc-2.95.3

Stefan Jones stefan.jones@multigig.com
Fri Oct 4 09:15:00 GMT 2002


Dear Sirs,
	After checking for leaks in C++ programs compiled with gcc I found the
following, both in 3.2and 2.95.3

Some memory was always leaked when using the STL, found using mtrace()
in glibc.

C++ code:

main.cpp is:
-------------
#include <list>
#include <mcheck.h>
using namespace std;
void boo() {
   list<int> l;
}
int main()
{
  mtrace();
  boo();
  return 0;
}
-------------
saj@sajX RotaryDB $ export MALLOC_TRACE="log"
saj@sajX RotaryDB $ /opt/gcc-3/bin/g++ --version
saj@sajX RotaryDB $ /opt/gcc-3/bin/g++ -g -o test main.cpp  saj@sajX
saj@sajX RotaryDB $ ./test
saj@sajX RotaryDB $ mtrace test log

Memory not freed:
-----------------
   Address     Size     Caller
0x0804a028    0x280  at
/root/gcc-3.2/i686-pc-linux-gnu/libstdc++-v3/libsupc++/new_op.cc:49

saj@sajX RotaryDB $ cat log
= Start
@ /opt/gcc-3/lib/libstdc++.so.5:(_Znwj+0x2e)[0x4007a80e] + 0x804a028
0x280


did a backtrace using gdb of the offending malloc:

(gdb) bt
#0  operator new(unsigned) (sz=640) at new_op.cc:46
#1  0x4009ef21 in std::__default_alloc_template<true,
0>::_S_chunk_alloc(unsigned, int&) (__size=16, __nobjs=@0xbffff4d8)
    at
/root/gcc-3.2/i686-pc-linux-gnu/libstdc++-v3/include/bits/stl_alloc.h:103
#2  0x4009ee3d in std::__default_alloc_template<true,
0>::_S_refill(unsigned) (__n=16) at
/root/gcc-3.2/i686-pc-linux-gnu/libstdc++-v3/include/bits/stl_alloc.h:525
#3  0x4009ea10 in std::__default_alloc_template<true,
0>::allocate(unsigned) (__n=640) at
/root/gcc-3.2/i686-pc-linux-gnu/libstdc++-v3/include/bits/stl_alloc.h:349
#4  0x08048bac in std::__simple_alloc<std::_List_node<int>,
std::__default_alloc_template<true, 0> >::allocate(unsigned) (__n=1) at
/opt/gcc-3/include/c++/3.2/bits/stl_alloc.h:224
#5  0x08048b02 in std::_List_alloc_base<int, std::allocator<int>,
true>::_M_get_node() (this=0x804a0c8) at
/opt/gcc-3/include/c++/3.2/bits/stl_list.h:238
#6  0x08048aa1 in _List_base (this=0x804a0c8, __a=@0xbffff5b4) at
/opt/gcc-3/include/c++/3.2/bits/stl_list.h:262
#7  0x08048a64 in list (this=0x804a0c8, __a=@0xbffff5b4) at
/opt/gcc-3/include/c++/3.2/bits/stl_list.h:361
#8  0x08048976 in main () at main2.cpp:7
#9  0x40125401 in __libc_start_main () from /lib/libc.so.6

Many thanks,
	Stefan Jones (plz CC me in replys)





More information about the Libstdc++ mailing list