QByteArray a("some data");// write access, thread #1void funcFromThread1(){ a = QByteArray(); // before assignment assume ref. count == 1 ...}// read-only access, thread #2void funcFromThread2(){ const QByteArray b = a; // potential access to released memory (pointed by a.d) ...}
const QByteArray b = a;
// lock-freeQByteArray a("some data");// write access, thread #1void funcFromThread1(){ const QByteArray tmp = a; // ref.count >= 2 while writing a a = QByteArray(); ...}// read-only access, thread #2void funcFromThread2(){ const QByteArray b = a; // safe access while writing a ...}
C++ (Qt)QByteArray &QByteArray::operator=(const QByteArray & other){ other.d->ref.ref(); if (!d->ref.deref()) qFree(d); d = other.d; return *this;}
C++ (Qt) if (!d->ref.deref()) <-- пробой здесь qFree(d);
d = other.d;