Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-18271

QList::clear deallocates memory

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 4.7.0
    • None
    • Windows XP 32-bits, Visual C++ 2008.
    • 04addd57742141f3f937722c1477ec7152bb888b

      The documentation states that only the destructor and assignment operator can deallocate memory:

      Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.

      However it happens in the clear() function as well, which is implemented using the assignment operator:

      template <typename T>
      Q_OUTOFLINE_TEMPLATE void QList<T>::clear()
      {
          *this = QList<T>();
      }
      

      You should either fix the doc, or modify clear() so that it doesn't deallocate memory. The second option would be more convenient, and more consistent with what the standard C++ library does. Currently, the only option for clearing a QList without deallocating memory is to iterate and remove elements one by one until the list is empty, which is probably not as optimal as clear() could be.

      If QList::clear() is modified, you should consider modifying QVector::clear() as well.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            dm Geir Vattekar
            laurentgom Laurent
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes