I have a QVector
QVector<const ClassA*> list;
and I want to know how it is what the best way to clean an QVector in this case.
ClassA is not a QObject.
I have a QVector
QVector<const ClassA*> list;
and I want to know how it is what the best way to clean an QVector in this case.
ClassA is not a QObject.
Have a look at the documentation of QVector.clear() - I think it answers your question quite well.
To preserve it, I copied it here:
void QVector::clear()
Removes all the elements from the vector.
Note: Until Qt 5.6, this also released the memory used by the vector. From Qt 5.7, the capacity is preserved. To shed all capacity, swap with a default-constructed vector:
QVector<T> v ...;
QVector<T>().swap(v);
Q_ASSERT(v.capacity() == 0);
or call squeeze().
I would write :
qDeleteAll(list);
list.clear();
I also advise you to read this post (as you may get something interesting from it) : Does C++11 allow vector<const T>?
EDIT (a few secondes after post) : My apologies to Pie_Jesu who posted a (very) similar comment on the question, which I did not read when I was writting this answer.
in this case clear() release only the pointer list from the vector but it not free the memory.
I try to do something like that
for (int index = 0; index < mList.count(); ++index )
{
const ClassA* value = const_cast<ClassA*>(mList[index ]);
delete value ;
}
I would simply suggest loop with a for each over it and delete every pointer. after that clear the vector
for(auto item : list){
delete item;
}
list.clear();
Or simply use QScopedPointer elements of classA in your list and simply call clear().