Memory leak using QVector

QVector<cLibraryRecord> Library;
...
Library.push_back(cLibraryRecord(ReaderFullName, BookGenre, BookTitle, AuthorsFullName, IssueDate, ReturnDate));
...
Library.remove(i);

QVector::remove() does not clear the memory. How can I clean the memory? Thanks in advance.

Upvotes: 0

Views: 4668

Answers (1)

alexisdm
alexisdm

Reputation: 29886

QVector.remove() always calls the destructor for the contained object, but the reserved size (returned by QVector::capacity()) doesn't shrink automatically when you remove elements.

You can use QVector::squeeze() to release the unused reserved memory.

But you can also have a memory leak in your class cLibraryRecord.

See Qt documentation for more details: Qt containers growth strategies.

Upvotes: 5

Related Questions