QIODevice: free memory when buffer is cleared
authorMartin Petersson <Martin.Petersson@nokia.com>
Mon, 11 Jun 2012 13:35:04 +0000 (15:35 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 26 Jun 2012 09:32:39 +0000 (11:32 +0200)
The QIODevicePrivateLinearBuffer does not deallocate any data on
readAll or clear. This fix will change the buffer so that
data is deallocated on clear, readAll and when read emptied the
buffer.

This is needed for QAbstractSockets that don't have
readBufferMaxSize set, as the buffer will grow but never
decrease in size when you read from it.

Change-Id: Iab42e40182f9ebe0739c99b2d1e820ce287dc931
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
src/corelib/io/qiodevice.cpp
src/corelib/io/qiodevice_p.h

index a60aee1..07634c3 100644 (file)
@@ -789,8 +789,10 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
             readSoFar += lastReadChunkSize;
             // fast exit when satisfied by buffer
             if (lastReadChunkSize == maxSize && !(d->openMode & Text)) {
-                if (d->buffer.isEmpty())
+                if (d->buffer.isEmpty()) {
+                    d->buffer.clear();
                     readData(data, 0);
+                }
                 return readSoFar;
             }
 
index 4819ec1..362ea4b 100644 (file)
@@ -78,8 +78,11 @@ public:
         delete [] buf;
     }
     void clear() {
-        first = buf;
         len = 0;
+        delete [] buf;
+        buf = 0;
+        first = buf;
+        capacity = 0;
     }
     int size() const {
         return len;
@@ -129,10 +132,9 @@ public:
         }
     }
     QByteArray readAll() {
-        char* f = first;
-        int l = len;
+        QByteArray retVal(first, len);
         clear();
-        return QByteArray(f, l);
+        return retVal;
     }
     int readLine(char* target, int size) {
         int r = qMin(size, len);