diff --git a/autotests/libs/collectionattributetest.h b/autotests/libs/collectionattributetest.h --- a/autotests/libs/collectionattributetest.h +++ b/autotests/libs/collectionattributetest.h @@ -32,6 +32,7 @@ void testDefaultAttributes(); void testCollectionRightsAttribute(); void testCollectionIdentificationAttribute(); + void testDetach(); }; #endif diff --git a/autotests/libs/collectionattributetest.cpp b/autotests/libs/collectionattributetest.cpp --- a/autotests/libs/collectionattributetest.cpp +++ b/autotests/libs/collectionattributetest.cpp @@ -240,3 +240,18 @@ QCOMPARE(parsed.identifier(), id); QCOMPARE(parsed.collectionNamespace(), ns); } + +void CollectionAttributeTest::testDetach() +{ + // GIVEN a collection with an attribute + Collection col; + col.attribute(Akonadi::Collection::AddIfMissing); + Collection col2 = col; // and a copy, so that non-const access detaches + + // WHEN + TestAttribute *attr = col2.attribute(Akonadi::Collection::AddIfMissing); + TestAttribute *attr2 = col2.attribute(); + + // THEN + QCOMPARE(attr, attr2); +} diff --git a/src/core/collection.h b/src/core/collection.h --- a/src/core/collection.h +++ b/src/core/collection.h @@ -565,10 +565,10 @@ Q_UNUSED(option); const QByteArray type = T().type(); + markAttributeModified(type); // do this first in case it detaches if (hasAttribute(type)) { T *attr = dynamic_cast(attribute(type)); if (attr) { - markAttributeModified(type); return attr; } //Reuse 5250 diff --git a/src/core/tag.h b/src/core/tag.h --- a/src/core/tag.h +++ b/src/core/tag.h @@ -217,10 +217,10 @@ Q_UNUSED(option); const QByteArray type = T().type(); + markAttributeModified(type); if (hasAttribute(type)) { T *attr = dynamic_cast(attribute(type)); if (checkAttribute(attr, type)) { - markAttributeModified(type); return attr; } }