PDA

View Full Version : Is there a memory leak here?



vinnzcrafts
19th March 2014, 11:28
Hello, I have some code I wanted to make sure I have no memory leaks in it so I ran it on my mac and opened activity monitor to see how much memory it was using, then I kept opening new windows(Editor class mentioned in code below) and closing it, and the memory usage increased when I opened them and didn't decrease as I closed. I tried the same thing in top using terminal to get the same result. Is it some lack of knowledge on my behalf on how activity monitor, top and even memory usage works? is it normal? or is there a leak in the code below

P.S. The Editor class allocates nothing dynamically (at least not more than child widgets and variables that were suppose to be disposed after the class is deleted right? ).

add_editor (when a new window is opened):



void MainControl::add_editor(int mode)
{
int i=0;
QPointer<Editor> *editor_aux=new QPointer<Editor> [editor_n+1];

while(i<editor_n)
{
editor_aux[i]=editor_array[i];
i++;
}

editor_aux[i]=new Editor(i,mode);
set_editor_conenctions(editor_aux[i]);

delete[] editor_array;

editor_array=editor_aux;
editor_n++;

if(editor_array[i]->start_response==2)
{
del_editor(editor_array[i]->id);
}
}


del_editor (when the window is closed):



void MainControl::del_editor(int id_arg)
{

if(editor_n>1)
{
int i=0;
int a=0;

QPointer<Editor> *editor_aux=new QPointer<Editor>[editor_n-1];

while(i<editor_n)
{
if(editor_array[i]->id==id_arg)
{
unset_editor_conenctions(editor_array[i]);
delete editor_array[i];
}else{
editor_aux[a]=editor_array[i];
a++;
}
i++;
}

delete[] editor_array;

editor_array=editor_aux;
editor_n--;
}

//This below is not needed because when the last window is closed the program closes
/*
if(editor_n==1){
editor_n=0;
delete editor_array[0];
}
*/
}

anda_skoa
19th March 2014, 13:23
Freed memory is not necessarily reclaimed by operating system right away.

You could run you program through valgrind and see if its memcheck tool reports any leaks.

Btw, your array handling code looks way more complicated than it needs to be, just use a resizable sequence container, e.g. a list or vector.

Cheers,
_

vinnzcrafts
19th March 2014, 14:39
Thank you very much, I will look into valgrind and something more simple

Cheers