Important: Please read the Qt Code of Conduct -

Qt Memory Deallocation

  • Hi all,

    I have a Qt program composed of objects dynamically created. These objects have derived from QObjects but not used parent argument at the constructor caller for example myQObject *mObject = new myQObject();

    Does Qt make the memory deallocation while QMainWindow has been closing or should we do delete operations such as C programmer?

    Thanks advance for your clarifications and helps,

  • If those objects do not have a parent, they won't be deleted automatically.

  • Lukas is right indeed, the objects will not be destroyed when classes aren't used anymore. Qt will clear all allocated memory when closing the program if you mean that.
    It is bad practice not to use the "parent" object to control the allocation/destruction of class memory. If you start writing larger programs you might even cause memory leaks! When using the new operator without the parent option always destroy the allocated memory. The only exception is the allocation in the Main class. This gets freed when the program closes.

Log in to reply