In a program I am currently working on, I have objects containing std::vectors
. The problem arise when I try to delete these objects, no memory is freed from the object.
I made a minimal program to test this and can't make it work correctly in this program either.
Here is the program I used to test.
#include<iostream>
#include<vector>
struct node{
std::vector<int> list;
bool isParent;
struct node* child;
~node(){
delete[] child;
}
node(){
isParent = false;
list.push_back(5); // comenting this line would make it work correctly
}
void divide_r(){
if (isParent){
for(int i = 0; i < 8; i++){
(child+i)->divide_r();
}
}else{
node *c;
c = new node[8];
child = &c[0];
isParent = true;
}
}
};
int main(){
node *root = new node;
for(int i = 0; i < 8; i++){
root->divide_r();
}
delete root;
sleep(10);
return 0;
}
So, if I push anything into the vector, I can't free up any memory.
I am using g++ on ubuntu if that matters. Am I doing anything wrong or should this work?
I have also tried to use different methods to free memory from "list" in the destructor , but as "list" will fall out of scope it should be freed anyway, I guess.
The program will use about 1.4GB of RAM, but nothing gets freed before after sleep and the program exits.