Access Violation After Call to QVector::append
-
Hello. I have recently run into an issue while using QVector. After loading a QVector up with a bunch of values, a call to append results in an access violation. I believe that the post at https://www.google.com/url?q=https://bugreports.qt-project.org/browse/QTBUG-10108&sa=U&ei=Q6xqUvuxDZP29gTky4G4CA&ved=0CB8QFjAJ&client=internal-uds-cse&usg=AFQjCNH95ZDbmMJ-rqtex_fRofdnyqF6CQ refers to the same behavior but they are interested in addressing a different piece of the issue. Examining the call stack reveals that there is a call to QByteArray::realloc which must fail because the result is a NULL pointer which Qt attempts to deallocate. This is obviously the source of the access violation but why did realloc fail?
This is what my code looks like (basically):
@
QVector<QVariant> getRowData();QVector<QVariant> allData;
QVector<QVariant> tempData;for (int r = 0; r < 67000; r++) {
tempData = getRowData();
for (int c = 0; c < tempData.size(); c++) {
allData.append (tempData[c]);
}
}
@
I'm actually reading data from a csv file so just imagine that getRowData() is filling a QVector with a row of values from the file.
The max values on the for-loops are approximately the dimensions of my csv file.
If I use QList instead of QVector then there are no problems. Any ideas of what is happening?Thanks.
-
Lists do not have the requirement of continuous memory. Vectors have this requirement. Typical vector implementations have to reserve a certain "amount of memory":http://qt-project.org/doc/qt-5.0/qtcore/qvector.html#capacity right at the beginning. Otherwise each append would require a complete new allocation of the vector's size + 1 element. I do not know the exact implementation of QVector, but it looks that you are hitting the capacity limit and probably capacity extension is basically doubling it. At a certain stage you simply hit the ceiling when the system is not able to extend and copy the old to the new vector.
-
Here you find details on the "growth strategies":http://qt-project.org/doc/qt-5.0/qtcore/containers.html#growth-strategies of QVector and QList
-
Thanks koahnig.