C
christophe.chazeau
Hi,
I have a problem with a really simple chunk of code which should work
but does obviously does not.
This chunk of code is just a POC aimed at finding a bug in a larger
project in which the same problem seems to occur.
Here the deal : when I run this piece of code, I expect all the memory
allocated by the "Test" object to be freed but what I observe is that
after the second sleep (after all the additions to the vector), the
memory taken by the process is 750Meg which is normal but after the
last sleep, the memory occupied by the process stays as high as 590Meg
when I expected it to be freed completely.
What exactly is wrong with this code ??
Thanx a lot in advance
Code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>
#include <iterator>
using namespace std;
class Test{
public :
unsigned char *data ;
Test(){
}
void Go(int taille)
{
data = (unsigned char*) malloc(taille);
printf("malloc : %p\n",data);
}
~Test()
{
printf("delete %p\n",data);
free(data);
}
};
vector<Test *> v;
int i = 0 ;
int main()
{
sleep(10);
for(i = 0;i<10000;i++)
{
Test *t = new Test();
t->Go(75000);
v.push_back(t);
}
sleep(10);
vector<Test *>::iterator it;
for(it=v.begin() ; it!=v.end();it++)
{
Test *t = *it;
delete(t);
}
sleep(10);
}
I have a problem with a really simple chunk of code which should work
but does obviously does not.
This chunk of code is just a POC aimed at finding a bug in a larger
project in which the same problem seems to occur.
Here the deal : when I run this piece of code, I expect all the memory
allocated by the "Test" object to be freed but what I observe is that
after the second sleep (after all the additions to the vector), the
memory taken by the process is 750Meg which is normal but after the
last sleep, the memory occupied by the process stays as high as 590Meg
when I expected it to be freed completely.
What exactly is wrong with this code ??
Thanx a lot in advance
Code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>
#include <iterator>
using namespace std;
class Test{
public :
unsigned char *data ;
Test(){
}
void Go(int taille)
{
data = (unsigned char*) malloc(taille);
printf("malloc : %p\n",data);
}
~Test()
{
printf("delete %p\n",data);
free(data);
}
};
vector<Test *> v;
int i = 0 ;
int main()
{
sleep(10);
for(i = 0;i<10000;i++)
{
Test *t = new Test();
t->Go(75000);
v.push_back(t);
}
sleep(10);
vector<Test *>::iterator it;
for(it=v.begin() ; it!=v.end();it++)
{
Test *t = *it;
delete(t);
}
sleep(10);
}