Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access Violation After Call to QVector::append
Qt 6.11 is out! See what's new in the release blog

Access Violation After Call to QVector::append

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        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

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Thanks koahnig.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt
          • Unsolved