QObject member variable destruction: deleteLater vs this
Unsolved
General and Desktop
-
What is a better way to handle the destruction of a QObject class member variable?
With a destructor (class A) or with a "this" and therefore rely on the parent destruction (class B)?class A{ public: ~A() { m_label->deleteLater(); } void function(){ m_label = new QLabel; } private: QLabel* m_label; }
class B{ public: void function(){ m_label = new QLabel(this); } private: QLabel* m_label; }
Thanks for help.
-
"this" is cleaner I think. It also lets you have the class system manage the memory as intended. It also does not keep you from deleting it earlier either. I really don't see downsides to "this". With deleteLater you will be riddling your code with calls.
-
Hi,
One thing that your code does not show is what are the base classes of class A and B. If you want to use them as parent of the members variable, they have to from a suitable base classe.