[Bug libstdc++/39237] New: Overloaded Operator delete not called

caroline dot rioux at ca dot ibm dot com gcc-bugzilla@gcc.gnu.org
Wed Feb 18 21:38:00 GMT 2009


Hello,

We use a unit testing framework which overloads operator new and operator
delete to track memory allocations and detect leaks.  According to it, vector's
push_back method allocates memory through operator new but does not release it
through operator delete.

I am not sure if this is because the memory is not released, is not released
through delete, or that the overloaded operator is simply not called.

The simple test case is as follows;

----
#include <vector>
#include <iostream>

int main (int argc, char ** argv) 
{
  {
        std::vector< int > objects;
        objects.push_back(1);
  }
  return 0;
}

static int allocatedBlocks = 0;

void* operator new(size_t size)
{
        allocatedBlocks++;
        void * toReturn = malloc(size);
        std::cerr << "operator new(size = " << size 
                  << "), returns: " << toReturn 
                  << " allocatedBlocks: " << allocatedBlocks << std::endl;
        return toReturn;
}

void operator delete(void* mem)
{
        if(mem == NULL)
          return;
        allocatedBlocks--;
        std::cerr << "operator delete(mem = " << mem << ") allocatedBlocks: "
<< allocatedBlocks << std::endl;
        free(mem);
}
-----

Using gcc 3.2.3, running this program yields the following output:

operator new(size = 320), returns: 0x503010 allocatedBlocks: 1

Using gcc 4.1.2, I get the expected:

operator new(size = 4), returns: 0x503010 allocatedBlocks: 1
operator delete(mem = 0x503010) allocatedBlocks: 0

I read in the FAQ that containers save memory for later re-use, but shouldn't
it be released when the vector goes out of scope? 

Please see the preprocessed source (attached) and the g++ information below.

--------------
/home/riouxc/temp> g++ -v -save-temps simple_vector.cpp 
Reading specs from /isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--enable-languages=c,c++ --disable-libgcj --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-47.3)
 /isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/cpp0 -lang-c++
-D__GNUG__=3 -D__DEPRECATED -D__EXCEPTIONS -v -iprefix
/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/ -D__GNUC__=3
-D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=3 -D__GXX_ABI_VERSION=102 -D__ELF__
-Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__
-D__unix -D__linux -Asystem(posix) -D__NO_INLINE__ -D__STDC_HOSTED__=1
-D_GNU_SOURCE -Acpu=x86_64 -Amachine=x86_64 -D__x86_64 -D__x86_64__
-D__SIZE_TYPE__=unsigned long int -D__PTRDIFF_TYPE__=long int -D__tune_athlon__
-D__tune_athlon_sse__ -D__LONG_MAX__=9223372036854775807L -D__LP64__ -D_LP64
simple_vector.cpp simple_vector.ii
GNU CPP version 3.2.3 20030502 (Red Hat Linux 3.2.3-47.3) (cpplib) (x86-64
Linux/ELF)
ignoring nonexistent directory "/isv/gnu/bin/../x86_64-redhat-linux/include"
ignoring nonexistent directory "/usr/x86_64-redhat-linux/include"
ignoring duplicate directory "/usr/include/c++/3.2.3"
ignoring duplicate directory "/usr/include/c++/3.2.3/x86_64-redhat-linux"
ignoring duplicate directory "/usr/include/c++/3.2.3/backward"
ignoring duplicate directory
"/usr/lib/gcc-lib/x86_64-redhat-linux/3.2.3/include"
#include "..." search starts here:
#include <...> search starts here:
 /isv/gnu/bin/../include/c++/3.2.3
 /isv/gnu/bin/../include/c++/3.2.3/x86_64-redhat-linux
 /isv/gnu/bin/../include/c++/3.2.3/backward
 /isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/include
 /usr/local/include
 /usr/include
End of search list.
 /isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/cc1plus -fpreprocessed
simple_vector.ii -quiet -dumpbase simple_vector.cpp -version -o simple_vector.s
GNU CPP version 3.2.3 20030502 (Red Hat Linux 3.2.3-47.3) (cpplib) (x86-64
Linux/ELF)
GNU C++ version 3.2.3 20030502 (Red Hat Linux 3.2.3-47.3) (x86_64-redhat-linux)
        compiled by GNU C version 3.2.3 20030502 (Red Hat Linux 3.2.3-47.3).
 as -V -Qy -o simple_vector.o simple_vector.s
GNU assembler version 2.15.92.0.2 (x86_64-redhat-linux) using BFD version
2.15.92.0.2 20040927
 /isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/collect2 --eh-frame-hdr
-m elf_x86_64 -Y P,/usr/lib64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/../../../../lib64/crt1.o
/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/../../../../lib64/crti.o
/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/crtbegin.o
-L/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3
-L/isv/gnu/bin/../lib/gcc-lib -L/usr/lib/gcc-lib/x86_64-redhat-linux/3.2.3
-L/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/../../../../lib64
-L/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/../../..
-L/usr/lib/gcc-lib/x86_64-redhat-linux/3.2.3/../../../../lib64
-L/usr/lib/gcc-lib/x86_64-redhat-linux/3.2.3/../../.. -L/lib/../lib64
-L/usr/lib/../lib64 simple_vector.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s
-lgcc /isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/crtend.o
/isv/gnu/bin/../lib/gcc-lib/x86_64-redhat-linux/3.2.3/../../../../lib64/crtn.o


-- 
           Summary: Overloaded Operator delete not called
           Product: gcc
           Version: 3.2.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: caroline dot rioux at ca dot ibm dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39237



More information about the Gcc-bugs mailing list