cnm
cnm

Reputation: 113

How to release memory (re-)allocated by QByteArray.append()

How to release the memory (re-)allocated by append() in QByteArray ??

I defined a QByteArray in the C++ header file:

QByteArray buffer;

and initialize in cpp file something like this:

Root:Root():
    buffer(QByteArray())
{
}

Root:Append(const QByteArray &chunk)
{
    buffer.append(chunk);
}

Root:Test()
{
    // Make the number bigger if 3000 is not big enough for 
    // append() to trigger reallocate memory
    for (int i = 0; i < 3000; i++){
        Append("xxxxxxxxxxxxxx");
    }
    // Private Bytes and Working set get increased

    buffer.clear();
    // Private Bytes and Working set DOES NOT get decreased
    // How to release the memory (re-)allocated by append() 
    // in QByteArray ??
}

Upvotes: 0

Views: 847

Answers (1)

Alexander V
Alexander V

Reputation: 8718

The below will move new empty QByteArray into old one effectively releasing all memory allocated:

myQByteArray = QByteArray();

Thanks to the assignment operator:

QByteArray &QByteArray::operator=(QByteArray &&other)

Move-assigns other to this QByteArray instance. This function was introduced in Qt 5.2.

Upvotes: 2

Related Questions