开发者

Element is removed from QList but static counter of existing objects doesn't decrease

开发者 https://www.devze.com 2022-12-20 21:15 出处:网络
I have question about removing element from QList. \"myclass.h\": class node2D : public QObject { Q_OBJECT

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 <<开发者_如何学Go; "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


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.


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.


In the QList you only store the pointers to an object. So if you clear the list you remove the reference to this pointer (aka the memory address). If you want to clear the object it self you can use smart pointers. In fact, you should pretty much always use smart pointers.

Try this instead

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


    node2D controlNode; //if you mixt QSharedPointer and objects like this, maybe you are better off using QExplicitelySharedDataPointer or QSharedDataPointer

    for (i = 0 ;i < max_el ; i++)
    {
        Nlist.append(QSharedPointer<node2D>(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;  not needed anymore since the list is not a pointer
    cout << "Number of nodes after removing: " << controlNode.checkCount() << endl;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消