You are invited to Log in or Register a free Frihost Account!

Qt: Does this constitute a memory leak or not?

Fire Boar
QList<QStandardItem *> lItems;
while (
   lItems.append(new QStandardItem(qData.value(0).toString()));
   lItems.append(new QStandardItem(qData.value(1).toString()));
   lItems.append(new QStandardItem(qData.value(2).toString()));
   lItems.append(new QStandardItem(qData.value(3).toString()));
   lItems.append(new QStandardItem(qData.value(4).toString()));

qData here is a QSqlQuery that has just been executed. As you can see, the QStandardItem* pointers aren't kept at all, just create the object, pass it to QList::append() and then forget about them. My question is, is this okay? Does QList or (more likely) QStandardItemModel* take ownership of any pointers passed to it and delete them on destruction? Or do I have a horrendous memory leak? oModel is a QStandardItemModel* by the way.
QList does not delete the pointed objects. You have to handle the destruction yourself.

You don't have to hold pointers when you create them. You can loop through the list, get the pointers and delete the objects, then clear the list.

The reason why QList does not delete the pointed objects is that it does not even know you are storing a pointer. It is a templated list which can hold objects of any type, including pointers.
Fire Boar
Ouch. Thanks for the info, but what about QStandardItemModel? Does that loop through the rows or do I have to do so manually?
appendRow takes a list as input. You don't have to loop manually. Here is the function signature.

void QStandardItemModel::appendRow(const QList<QStandardItem *> & items)

Actually looks like QStandardItemModel deletes all the items appended it. So I guess you don't really have to delete them.
i dont know much about QT memory management but if u allocate memory to pointers then u should also free it up
Fire Boar
umeshtangnu wrote:
i dont know much about QT memory management but if u allocate memory to pointers then u should also free it up

Qt is very different.

#include <QObject>

void main()
  QObject * ob1 = new QObject();
  QObject * ob2 = new QObject(ob1);
  ob2 = new QObject(ob1);
  ob2 = new QObject(ob1);
  ob2 = 0;
  delete ob1;

Blatant memory leak in vanilla C++ but for Qt? In fact, all the memory is freed in the final delete statement. That's because QObject (inherited by pretty much everything) keeps pointers to each child assigned to it, so all three of the QObjects created initially by the ob2 pointer are pointed to internally.

I'm pretty sure that QStandardItemModel maintains pointers to its data.
check this


QStandardItemModel::~QStandardItemModel ()

Destructs the model. The model destroys all its items.

That is for QT 4.2. Not sure about older versions.
Fire Boar
Aha. Perfect. That's irrefutable - I can't believe I missed that.
Related topics
BF2 Prob
(official) Firefox & Thunderbird 1.5 Beta2 Now Available
My Computer Keeps Freezing Up When It Loads Too Much...?
Internet Explorer Bug
Staff wanted
Firefox CPU Usage 100%, why ?
Background Processes in Windows XP
FireFox 2.0 or Firefox 1.x ?
Which Dreamweaver version do you use?
anybody have idea on memory leak?..
IE 8 vs firefox 3
firefox get slower and slower after using it for a while.
What is eating my memory???
Memory Recovery & Retention For Firefox
Reply to topic    Frihost Forum Index -> Scripting -> Others

© 2005-2011 Frihost, forums powered by phpBB.