PDA

View Full Version : QAbstractItemModel object memory management



kaswinikumar
23rd October 2010, 08:32
Who deletes the memory related to the argument of QSortFilterProxyModel::setSourceModel() function?

I saw code that passed the address of an object allocated on heap as argument to this function, but could not understand who was freeing that memory.

Any information would be appreciated.

Thanks.

Lykurg
23rd October 2010, 08:36
I saw codeWould be nice to see that code. But in general QSortFilterProxyModel::setSourceModel() does not take the ownership. so probably the model was created with a parent object and thus the model gets deleted if the parent object gets deleted. Thats a feature of Qt.

EDIT: From the docs of QObject:
QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren().

kaswinikumar
23rd October 2010, 08:44
Thanks Lykurg. Unfortunately I am not sure if I can post the exact code here... but there were repeated invocations of setSourceModel() function each time with a new heap based model object [each one created with out a parent specified] and the existing model object (set in the previous invocation of that function) is not cleaned up anywhere. Even Valgrind reported that as leak, but I just wanted to know if I was missing something. Looks like it is a valid leak and I should delete those objects explicitly.

wysota
23rd October 2010, 08:56
If there is no parent, then the model will not be deleted by itself. It would be best just to make it have a parent that gets deleted around at the same time when you don't need the model (usually there such a situation exists). Otherwise you have to explicitly delete the object yourself.

kaswinikumar
23rd October 2010, 09:37
Thanks Wysota.