PDA

View Full Version : Deleting QApplication and QMainWindow Objects ?



jenlly
7th May 2016, 09:16
I understand that the QWidget will delete its child widgets appropriately. What about the objects of QMainwindow, the super parent and QApplication. Do I need to delete them explicitly to clear the memory? If it is an executable, (i.e. the QMainWindow and QApplication objects are created in main function), it may be fine because the entire application is going to exit anyway. But what happens in case of a dll (i.e. QMainWindow and QApplication objects are created inside an exported dll-function)?
In this case, The dll-function is invoked from a parent application and later, QMainWindow is closed by the user. Remember that the parent application is still running. When the same session of parent application invokes the dll-function again, the Qt objects are created newly.
So is there any chance of memory issues when it happens multiple time?

anda_skoa
7th May 2016, 09:55
In the case of a normal application, you will most likely create QApplication and your main window instance on the stack of main(), so they get deleted when main() ends.

In case of it being hidden inside a DLL, you could set the Qt::WA_DeleteOnClose attribute on the window so it gets deleted when it gets closed.
You could also connect the application object's lastWindowClosed() signal to its own deleteLater() slot, or you keep just this object around for further use.

Cheers,
_