[Development] What am I not seeing in the QByteArray::capacity test failure?
Thiago Macieira
thiago.macieira at intel.com
Wed Apr 23 09:24:54 CEST 2014
Em qua 23 abr 2014, às 09:01:39, Olivier Goffart escreveu:
> > 1858 qba.resize(capacity);
> >
> > [...]
> >
> > 1863 QCOMPARE(qba.capacity(), capacity);
> >
> > [...]
> >
> > 1866 copy = qba;
>
> copy was still holding the original data. But there, it is assigned to
> detached version of itself, therefore the original data get freed.
Yes, I see it now:
1850 char *data = qba.data();
1858 qba.resize(capacity);
1859
1860 QByteArray copy = qba;
Now, copy.d == qba,d && refcount == 2.
1861 qba.reserve(capacity / 2);
reserve() will hit isShared() == true, so qba will detach in order to reserve.
So copy.d.ref == 1. Note how the tests don't check if qba.constData() == data
again:
1862 QCOMPARE(qba.size(), capacity); // we didn't shrink the size!
1863 QCOMPARE(qba.capacity(), capacity);
1864 QCOMPARE(copy.capacity(), capacity);
1865
1866 copy = qba;
And, like you said, copy.d gets freed here. One fix is to create a new
QByteArray on this line, instead of reusing copy.
Or to invert: qba = copy; which is probably what the test intended, given the
rest of the comparisons:
1867 qba.reserve(capacity * 2);
1868 QCOMPARE(qba.size(), capacity);
1869 QCOMPARE(qba.capacity(), capacity * 2);
1870 QCOMPARE(copy.capacity(), capacity);
1871 QVERIFY(qba.constData() != data);
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
More information about the Development
mailing list