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.
One reason might be you are creating the
Loginwidget again and again without deleting it.I believe your Login is a
QWidget, Dashboard is aQDialog. Instead ofclose(), just hide the Login widget byhide()orsetvisible(false).In the
closeEvent()of the DashBoard givedone(someIntValue);While accessing DashBoard, instead of show() give
if the
returnValueissomeIntValue,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 thedestroyed( QObject * obj = 0 )signal of the DashBoard and in the corresponding slotshow()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.