Solved How To Release Memory ??
-
I have Created a simple QT application for my university assignment. What i have done is pop up a new QManinWindow from a Above QMainWindow. Check the below code you will figure it out
Note pad is also a QMainWindow object
My Problem is when I'm creating the object it takes some memory from the ram but when I'm closing it (pop up window) memory is not releasing. When each time I'm pressing a button memory is allocated but application does not relese the memory when im closing it. Please check the main screen of the app.
i just want to know how to release that memory. I have tried so many things but nothing worked well.
-
Hi and welcome to devnet,
If you don't reuse that widget, you can use the Qt::WA_DeleteOnClose which will trigger the deletion of the widget when you close it.
-
You could create your notepad outside the button event (below the line where your MainWindow UI is set). Then you have only one instance of your notepad window. OnButtonClick you can show/execute or hide it and delete it with you MainWindow destructor.
In this approach the same window shows up, every time you click the button (disadvantage: If you change something in your NotePadWindow, for example a slider position, and close the window, the slider is still at this position when you reopen it).mainwindow.cpp
// Inside MainWindow Constructor NotePad *m_notepad = new NotePad(this); // ... // ... // onButton Click / Open NotePadWindow { m_notepad->exec(); } // MainWindow destructor { delete m_notepad; delete ui; }
Or you allocate your NotePad on stack, if you are not planning to use the NotePad somewhere else. The memory gets freed automatically, when the objects' destructor is called (by X'ing the Window or Clicking a Close Btn).
This creates a new window with defaults, when the button is clicked.{ NotePad notepad(this); notepad.exec(); // or notepad.show(); }
Everytime you allocate memory on your heap (with the "new" keyword), you have to make sure, that you delete it, when it's no longer needed.
-
i hate tried the last thing that you specified mate. But once i do it and click the button, pop up windows(note pad) pops up and get killed.
That means when i click the button to show the notepad it shows up and get killed instantly.
-
@Lakith Don't set your main window as parent of NotePad: when parent is deleted the child is deleted as well!
See http://doc.qt.io/qt-5/objecttrees.html -
-
@Lakith said in How To Release Memory ??:
i hate tried the last thing that you specified mate. But once i do it and click the button, pop up windows(note pad) pops up and get killed.
That means when i click the button to show the notepad it shows up and get killed instantly.
I guess you used "show()" instead of "exec()"?!
"show()" lets your NotePad Window appear and continues with main loop routine... and because there is nothing more to do inside your buttonClick function, your NotePad is freed instantly and disappears.
"Exec()" stops your main loop and creates a new one inside your NotePad Window. Your main program will continue, if you close the NotePad.Why are you using two QMainWindow-type classes? QMainWIndow provides additional menu- / toolbars per default... If you dont need them in your NotePadWindow, declaring at as a QDialog or QWindow would make things easier.
-
@Pl45m4 said in How To Release Memory ??:
your NotePad is freed instantly and disappears
This is not the case here as he allocates NotePad on the heap.
-
Yes, but I if you create it on stack :)
Tried to show him other ways to allocate space for the NotePad class