diff options
-rw-r--r-- | src/corelib/tools/qvector.h | 4 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qvector/tst_qvector.cpp | 7 |
2 files changed, 10 insertions, 1 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 07c66bc393..e263b99c02 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -153,7 +153,9 @@ public: const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t); if (cit == ce) return 0; - const iterator e = end(), it = std::remove(c2m(cit), e, t); + // next operation detaches, so ce, cit may become invalidated: + const int firstFoundIdx = std::distance(this->cbegin(), cit); + const iterator e = end(), it = std::remove(begin() + firstFoundIdx, e, t); const int result = std::distance(it, e); erase(it, e); return result; diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 9a79d48472..256fa5e507 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -1500,11 +1500,18 @@ void tst_QVector::remove() const QVERIFY(myvec.removeOne(val2)); QCOMPARE(myvec, QVector<T>() << val1 << val3 << val1 << val3 << val1 << val2 << val3); + QVector<T> myvecCopy = myvec; + QVERIFY(myvecCopy.isSharedWith(myvec)); // removeAll() QCOMPARE(myvec.removeAll(val4), 0); + QVERIFY(myvecCopy.isSharedWith(myvec)); QCOMPARE(myvec.removeAll(val1), 3); + QVERIFY(!myvecCopy.isSharedWith(myvec)); QCOMPARE(myvec, QVector<T>() << val3 << val3 << val2 << val3); + myvecCopy = myvec; + QVERIFY(myvecCopy.isSharedWith(myvec)); QCOMPARE(myvec.removeAll(val2), 1); + QVERIFY(!myvecCopy.isSharedWith(myvec)); QCOMPARE(myvec, QVector<T>() << val3 << val3 << val3); // remove rest |