tags:

views:

145

answers:

2

I have question about removing element from QList.

"myclass.h":

class node2D : public QObject
{
    Q_OBJECT

public:
    node2D(){++s_NCount;};
    ~node2D(){--s_NCount;};

    int checkCount(){return s_NCount;};

private:
    static int s_NCount;
};

"myclass.cpp":

int node2D::s_NCount = 0;

"main.cpp":

void main()
{
    int i,max_el(4);
    QList<node2D*> *Nlist;
    Nlist = new QList<node2D*>;

    node2D controlNode;

    for (i = 0 ;i < max_el ; i++)
    {
        Nlist->append(new node2D);
    }

    cout << "Nlist size before: " << Nlist->size() << endl;
    cout << "Number of nodes before removing: " << controlNode.checkCount() << endl;

    Nlist->clear();
    cout << "NList size after: " << Nlist->size() << endl;
    delete Nlist;
    cout << "Number of nodes after removing: " << controlNode.checkCount() << endl;
}

After executing I get:

  • NList size before: 4
  • Number of nodes before removing: 5
  • NList size after: 0
  • Number of nodes after removing: 5

What's bothering me is the fact that number of node2D objects is still 5 instead of 1.

Of course it can be managed like this:

for (i = 0; i < Nlist->size(); i++)
{
    delete (*Nlist)[i];
}
Nlist->clear();

but shouldn't node2D objects be automatically deleted while Nlist->clear()?

Or does it only happen when there is parent-child relation?

Thanks in advance,

Pawel

+2  A: 

I'm not familiar with QT in particular, but normally generic containers do not delete contained pointers on destruction, since there are many cases in which that would not be what the programmer wants. It works this way in the C++ STL and I can't imagine it would work differently in QT.

If you had inserted Node2D objects instead of pointers to Node2D objects, then the container would have made a copy of the contained objects and would have destructed those copies when the list destructed. When you insert a pointer, it makes a copy of the pointer, not the object, and so it remains up to you (the inserter) to manually manage the lifetime of the pointed-to object.

Tyler McHenry
That's right, but unfortunately I need to use pointers. Thanks
Moomin
+3  A: 

but shouldn't node2D objects be automatically deleted while Nlist->clear()?

Not at all. What if i want to use these objects somewhere else, which is the case mostly for me. Managing the objects pointed by the pointers you add to the list is your concern, not QList's. Managing the copies of these pointers is on the other hand, QList's concern.

erelender
And how about parent(QList)-child(every node2D) relation? Would it happen automatically then?
Moomin
What exactly do you mean by "child relation"?
Tyler McHenry
If you make node2D objects child of the NList, then they will be deleted when Nlist is deleted, but not when NList is cleared.
erelender
@Tyler: Moomin talks about the parent-child relationship of QObject instances in Qt, which is not present in the code snippet above.
erelender
parent-child: setting Nlist as a parent of any node2d created within this list. I think that it is one of QT features that deleting parent object should delete all its childs
Moomin
QObjects do handle the deletion of their children, but QList isn't a QObject, so that isn't going to help you.
Parker