fix(QObjectUniquePtr): delete currently pointed object when moving

This commit is contained in:
Julien Cabieces 2025-09-29 16:33:34 +02:00 committed by Nyall Dawson
parent beb3e987f0
commit f580afd7a6
2 changed files with 13 additions and 0 deletions

View File

@ -91,6 +91,7 @@ class QObjectUniquePtr
QObjectUniquePtr &operator=( QObjectUniquePtr &&other ) noexcept
{
delete mPtr.data();
mPtr = other.mPtr;
other.clear();
return *this;

View File

@ -136,6 +136,18 @@ void TestQObjectUniquePtr::testVector()
QCOMPARE( objects.at( 0 )->objectName(), "TESTA" );
QCOMPARE( objects.at( 1 )->objectName(), "TESTB" );
int nbObjectDestroyed = 0;
auto onDestroyed = [&nbObjectDestroyed]() { nbObjectDestroyed++; };
connect( objects.at( 0 ), &QObject::destroyed, this, onDestroyed );
connect( objects.at( 1 ), &QObject::destroyed, this, onDestroyed );
objects.erase( objects.cbegin() );
QCOMPARE( objects.size(), 1 );
QCOMPARE( nbObjectDestroyed, 1 );
QCOMPARE( objects.at( 0 )->objectName(), "TESTB" );
}