Need to free the contents of a QList?

I have a Qlist full of objects created dynamically. Before ending my program, I call myqlist.clear ()

My question is: does this also delete (for free) the objects that are in the list? Valgrind gives me some lost blocks, and I wonder if I haven’t understood how the qlist clear method works.

Or do I need to iterate over qlist and delete each object?


Update: I can confirm that mylist.erase (iterator) removes the item from the list, but DOES NOT release the dynamically allocated object. (An object is a dynamically created class). Very strange! I switched from Qlist to QLinkedList, but with the same results. Remember that my QLinkedList is a QLinkedList <myclass> not a QLinkedList <* myclass>

Here is the real code if someone can find what I am doing wrong:

// Here I define a couple important items.  Note that AMISendMessageFormat is a class
typedef QLinkedList<AMISendMessageFormat> TSentMessageQueue;
TSentMessageQueue m_sentMessageQueue;

// Here I create the message and append to my QLinkedList
AMISendMessageFormat *newMessage = new AMISendMessageFormat(messageToSend);
m_sentMessageQueue.append(*newMessage); 

// Here I delete
for (TSentMessageQueue::Iterator sMessagePtr = m_sentMessageQueue.begin(); sMessagePtr != m_sentMessageQueue.end(); )
{
    sMessagePtr = m_sentMessageQueue.erase(sMessagePtr);  
    qDebug() << "Sent size after erase: " << m_sentMessageQueue.size();  // Confirmed linked list is shrinking in size
}

And after iterating through the list and erasing, valgrind shows that each of the AMISendMessageFormat objects is a lost block!

I suspect this is due to erasing inside the loop using an iterator ... but I can't think it over!


See the detailed solution below ... the problem was that the append function makes a copy and adds it to the list ... I though it added the actual object (not the copy) ... so the problem was the new "copy .

+4
source share
3 answers

, newMessage. ! . :

// Best

m_sentMessageQueue << AMISendMessageFormat(messageToSend);

// Same, more writing

AMISendMessageFormat newMessage(messageToSend);
m_sentMessageQueue << newMessage;

// Rather pointless allocation on the heap

QScopedPointer<AMISendMessageFormat> newMessage(new AMISendMessageFormat(messageToSend));
m_sentMessageQueue << *newMessage; 

, . . , AMISendMessageFormat - ++ , .

- , , , . Qt ++ , . , , , , QSharedPointer.

, .

  • raw-point-to-things, , , , clear(). A QList , , . ++ , , NO-OP.

  • QSharedPointer std::shared_ptr , , clear(). - :)

  • , ++, .

QObject QList, "" QObjects - .

:

QList<QString> stringList1;
QList<QSharedPointer<QString> > stringList2;

stringList1 << "Foo" << "Bar" << "Baz";
stringList2 << new QString("Foo") << new QString("Bar") << new QString("Baz");

Q_ASSERT(stringList1.at(0) == *stringList2.at(0));
stringList1.clear();
stringList2.clear(); // no memory leaks

, :

QList<QString*> stringList3;
stringList3 << new QString("Foo") << new QString("Bar") << new QString("Baz");
stringList3.clear();

, QList ++ RAII. , , . , clear() , , . . main(), , .

int main() {
    QList<QString> stringList1;
    stringList1 << "Foo" << "Bar" << "Baz";
    return 0;
}
+4
qDeleteAll(list.begin(), list.end());
+2

. Qt , . , .

, . Qt , .

0
source

Source: https://habr.com/ru/post/1523925/


All Articles