I have found that small sized allocations can't be deleted in c++ , probably
because a bug in glibc. I didn't know where to report this, so i decided that i
will report it here too. 
This behaviour affects the very basic map container too. For example by using
map<int, int> with many elements. 
The allocated memory could be used by other parts of the program later if they
fit in (for example by using another map<int, int>), but it will never be freed
to be able to use it by other processes till the end of the program.

In the following program i represented a map node with a struct. It will
allocate a lot of these struct and later it tries to delete them, without
success. If you uncomment the "big int array" in the struct, then it will
delete them as you would except normally. I compiled this code with every g++
in ubuntu with the same result.

#include <vector>
#include <map>
#include <iostream>

using namespace std;

struct node
{
   //int big[20];
   int a,b,c,d,e;
};

int main(int argc, char *argv[]) {
   const int num=10000000;
   {
        vector<node* > pointers;
        pointers.reserve(num);
        for(int i=0;i!=num;++i){
                node* p=new node;
                pointers.push_back(p);
        }
        cout << "end of allocating" << endl;
        char c;
        cin >> c;

        for(int i=0;i!=num;++i){
                node *p=pointers[i];
                delete p;
        }
   }

   cout << "nodes are deleted" << endl;
   char d;
   cin >> d;
   cout << "bye" << endl;
}

I checked malloc.c in glibc source. I found that under 64 bytes it won't
automatically release memory back to the kernel for obvious reasons, but if the
so wasted memory become large it will release them, by calling malloc_trim. I
tried to call it manually after deleting these nodes, and it worked. But this
kind of code is not portable ant most importantly not c++.

Sorry if i send this bug report at the wrong place. (this is my first)
Simon Rácz


-- 
           Summary: delete doesn't always delete
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: simonracz at gmail dot com


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

Reply via email to