diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 4eb90ac35e..65ebc296d4 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -130,19 +130,22 @@ public: return d; } + bool needsDetach() const + { + return (!d->isMutable() || d->ref.isShared()); + } + void setSharable(bool sharable) { - // Can't call setSharable on static read-only data, like shared_null - // and the internal shared-empties. - if (d->alloc == 0 && d->size == 0) { - d = Data::allocate(0, sharable - ? QArrayData::Default - : QArrayData::Unsharable); - return; + if (needsDetach()) { + Data *detached = clone(sharable + ? d->detachFlags() & ~QArrayData::Unsharable + : d->detachFlags() | QArrayData::Unsharable); + QArrayDataPointer old(d); + d = detached; + } else { + d->ref.setSharable(sharable); } - - detach(); - d->ref.setSharable(sharable); } void swap(QArrayDataPointer &other) @@ -158,7 +161,7 @@ public: bool detach() { - if (!d->isMutable() || d->ref.isShared()) { + if (needsDetach()) { Data *copy = clone(d->detachFlags()); QArrayDataPointer old(d); d = copy; diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index 641516e0d0..7e679704c8 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -175,7 +175,7 @@ public: if (size() == newSize) return; - if (d->ref.isShared() || newSize > capacity()) { + if (d.needsDetach() || newSize > capacity()) { SimpleVector detached(Data::allocate( d->detachCapacity(newSize), d->detachFlags())); if (newSize) { @@ -211,7 +211,7 @@ public: return; T *const begin = d->begin(); - if (d->ref.isShared() + if (d.needsDetach() || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), @@ -232,7 +232,7 @@ public: if (first == last) return; - if (d->ref.isShared() + if (d.needsDetach() || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), @@ -272,7 +272,7 @@ public: T *const begin = d->begin(); T *const where = begin + position; const T *const end = begin + d->size; - if (d->ref.isShared() + if (d.needsDetach() || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)),