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*)));
}
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*)));
}
To copy to clipboard, switch view to plain text mode
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
So I uses the protected function destroy(true, true) which will deallocate QDialog and all his childs.

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;
}
void MainWindow::deallocOrderAddWindow(OrderAddWindow *window)
{
qDebug("MainWindow::deallocOrderAddWindow: Starting deallocating procedure");
//this->destroy(false, true);
delete window;
}
To copy to clipboard, switch view to plain text mode
So as you can see, in both runs the memory keeps on growing..... how can I change that, or is it normal?
Greetings,
Cyberboy
Bookmarks