QIODevice: free memory when buffer is cleared

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>
This commit is contained in:
Martin Petersson 2012-06-11 15:35:04 +02:00 committed by Qt by Nokia
parent 1ce203d05a
commit a9c398096b
2 changed files with 9 additions and 5 deletions

View File

@ -789,8 +789,10 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
readSoFar += lastReadChunkSize; readSoFar += lastReadChunkSize;
// fast exit when satisfied by buffer // fast exit when satisfied by buffer
if (lastReadChunkSize == maxSize && !(d->openMode & Text)) { if (lastReadChunkSize == maxSize && !(d->openMode & Text)) {
if (d->buffer.isEmpty()) if (d->buffer.isEmpty()) {
d->buffer.clear();
readData(data, 0); readData(data, 0);
}
return readSoFar; return readSoFar;
} }

View File

@ -78,8 +78,11 @@ public:
delete [] buf; delete [] buf;
} }
void clear() { void clear() {
first = buf;
len = 0; len = 0;
delete [] buf;
buf = 0;
first = buf;
capacity = 0;
} }
int size() const { int size() const {
return len; return len;
@ -129,10 +132,9 @@ public:
} }
} }
QByteArray readAll() { QByteArray readAll() {
char* f = first; QByteArray retVal(first, len);
int l = len;
clear(); clear();
return QByteArray(f, l); return retVal;
} }
int readLine(char* target, int size) { int readLine(char* target, int size) {
int r = qMin(size, len); int r = qMin(size, len);