PDA

View Full Version : Destroy object



Viper666
4th January 2013, 20:23
Hi,
i programming in qt already sometime and allway i am worried about memory leaks. So this is my question:
In classic Qt programming i used to use something like:
MyObject *object = new MyObject(this);. Is this correct? Will object be delete automatic or i created memory leak?

Thanks

wysota
4th January 2013, 21:38
It depends what "this" and "MyObject" are. If they are both descendants of QObject then there is no memory leak however object will not be deleted until "this" is deleted.

alizadeh91
4th January 2013, 21:40
this is correct but to be sure about not having memory leak just delete objects whenever they are no longer needed

d_stranz
4th January 2013, 22:16
to be sure about not having memory leak just delete objects whenever they are no longer needed

This is not correct! As Wysota said, if an object descends from QObject, and is created with another QObject class as its parent, then the parent, not the user, will almost always be responsible for deleting the object. If you manually delete a QObject that has a parent, the destructor will be sure to remove it from the parent's list of children, but nevertheless, this is not good Qt practice. Let QObject handle the child lifetimes, as it was designed to do and you won't get into trouble.

Ordinary C++ object instances (not derived from QObject) follow the normal C++ rules for creation and deletion, but QObject classes are different because of the parent-child relationship that is established when a QObject instance is created with a parent (or assigned one later).

The major exception to this Qt rule is in the Graphics / View architecture: A QGraphicsItem instance that is added to a QGraphicsScene but then later removed from it through a call to QGraphicsScene::removeItem() must be manually deleted. However, instances that are permanently added to the scene do not have to be deleted, because the scene is in charge of their lifetimes and they will be deleted when the scene instance is deleted.

wysota
4th January 2013, 22:29
The major exception to this Qt rule is in the Graphics / View architecture: A QGraphicsItem instance that is added to a QGraphicsScene but then later removed from it through a call to QGraphicsScene::removeItem() must be manually deleted. However, instances that are permanently added to the scene do not have to be deleted, because the scene is in charge of their lifetimes and they will be deleted when the scene instance is deleted.

This is really no exception. The same happens for Q{List,Table,Tree}WidgetItem instances and possibly everything else that loses its parentship (including QObject instances if you call setParent(0) on them).

d_stranz
4th January 2013, 22:52
The same happens for Q{List,Table,Tree}WidgetItem instances

Yes, true. I tend to overlook these cases. I guess I have rarely come across this in actual usage - most often this is hidden in the details of a model - view interaction, or, if I build a table manually, I just clear the former contents first then re-fill the whole thing. In my usage, QGraphicScene implementations tend to be more dynamic.

Viper666
5th January 2013, 14:43
Thanks :D That's what i need :D