views:

81

answers:

1

I'm trying to develop a new program. The work flow looks like this:

Login --> Dashboard (Window with menus) --> Module 1
                                        --> Module 2
                                        --> Module 3
                                        --> Module XXX

So, to open Dashboard from Login (a Dialog), I use

Dashboard *d = new Dashboard();
d->show(); 
close(); 

In Dashboard, I use these codes to reopen the Login if the user closes the Window (by clicking the 'X')

closeEvent(QCloseEvent *) 
{
    Login *login = new Login();
    login->show();
}

With a Task Manager opened, I ran the program and monitor the memory usage. After clicking open Dashboard from Login and closing Dashboard to return to Login, I noticed that the memory keeps increasing about 500 KB. It can goes up to 20 MB from 12 MB of memory usage by just opening and closing the window/dialog.

So, what did I do wrong here ? I need to know it before I continue developing those modules which will definitely eat more memory with my programming. Thanks in advance.

+1  A: 

One reason might be you are creating the Login widget again and again without deleting it.

I believe your Login is a QWidget, Dashboard is a QDialog. Instead of close(), just hide the Login widget by hide() or setvisible(false).

In the closeEvent() of the DashBoard give done(someIntValue);

While accessing DashBoard, instead of show() give

int returnValue = d->exec();

if the returnValue is someIntValue, show() the Login Widget.

This way you are avoiding the creation of the Login widget multiple times. But a lot other factors might be responsible for your memory usage, which could be found out only by going through the entire code.

Edit:

Since your Dashboard is a QMainWindow you can't call done(someIntValue); Instead try connecting the destroyed( QObject * obj = 0 ) signal of the DashBoard and in the corresponding slot show() your Login.. Of course you should have the instance of the DashBoard in the Login Dialog and the connection between the above signal and slot should be made in the Login.

Hope it helps.

liaK
**Login** is a QDialog and **Dashboard** is a QMainWindowIf I use `hide()` or `setVisible(false)`, I need to pass the variable from a class to class, right ? That's not very practical in my program since I might be calling other classes multiple times before clicking the close window button to return to **Login**I know I should try avoiding the creation of any widget multiple times, but shouldn't there be no problem if I created something that has been destroyed, in this case, did my widget released its used memory when it has been closed ?
aurorius
no i think you misunderstood. You don't have to pass objects here and there. you can hide the Login Dialog just by the call hide(), since you derived Login from a QDialog. To answer to your other question, the Dialog will be deleted only if you set the attribute manually for the Dialog as, Login->setAttribute(Qt::WA_DeleteOnClose);
liaK
I think using `setAttribute(Qt::WA_DeleteOnClose)` works. But could you please give an example on how to reopen the **Login** dialog when I'm in **Dashboard** (without declaring new object) ? I can't just `show()` it back since there's no object associated with it. That's why I had to create a new object (which in result increases the memory usage)
aurorius
check my edit in the answer i provided..
liaK