PDA

View Full Version : Memory management



cyberboy
12th June 2008, 14:41
Hi everybody,

I have a little question about memory management.
When you construct a QDialog you pass the parent object into the child object. So the child is released when you release the parent object.

In my current project I have a QMainWindow, a some QDialogs. Each time a person wants to add a order a QDialog is allocated, the QDialog is the parent of the orderRecord class which is deallocated perfectly when I destruct my QDialog. Only my QDialog remains in the memory. So when you're opening let's say the window a 1000 times in one day (although that's excessive) the memory amount keeps on growing.

I will explain how it works, when I'm creating a QDialog I'am creating a class with a pointer.


void MainWindow::openOrderAddWindow()
{
qDebug("open order add dialog");

OrderAddWindow *window = new OrderAddWindow(this);

Query *query = new Query(database->getConnection(), this);

//connect the save data signal to the add slot from query.
//add will add the record data returned by saveData into the database
connect(window, SIGNAL(saveData(OrderRecord&)),
query, SLOT(add(OrderRecord&)));
connect(query, SIGNAL(querySucceeded(bool)),
window, SLOT(queryResponse(bool)));
connect(window, SIGNAL(dealloc(OrderAddWindow*)),
this, SLOT(deallocOrderAddWindow(OrderAddWindow*)));
}


When the queryResponse signal is true, I know that everything went fine, so I can deallocate the orderRecord object which the query class uses to get data from. And I can destroy the QDialog.



void OrderAddWindow::queryResponse(bool succeeded)
{
qDebug("OrderAddWindw::queryResponse(): The data is saved in the database, so we can close the window");
//close window
close();
//destruct parent and childs
destroy(true, true);

this->startDeallocating();
}

So I uses the protected function destroy(true, true) which will deallocate QDialog and all his childs.

http://img294.imageshack.us/img294/550/picture4mi1.png
The image monitors the memory allocation. The second run is with destroy called.
The first run is when start Deallocating() is called which emits the signal dealloc, this signal is connected to a function in QMainWindow which will delete the QDialog.


void MainWindow::deallocOrderAddWindow(OrderAddWindow *window)
{
qDebug("MainWindow::deallocOrderAddWindow: Starting deallocating procedure");
//this->destroy(false, true);
delete window;
}

So as you can see, in both runs the memory keeps on growing..... how can I change that, or is it normal?

Greetings,

Cyberboy

cyberboy
12th June 2008, 16:49
I have some kind of solution, I delete the objects manually.
So I use the delete function of c++, is this the right way to solve this problem?...

greetings cyberboy

wysota
12th June 2008, 20:48
When you close a window, it is not being destroyed. You either should create the dialog on the stack and use QDialog::exec() to make it modal, delete the dialog manually or set the Qt::WA_DeleteOnClose attribute on the dialog so that it gets deleted after being closed.