From e667624371613fad17b09d106e2eb80d581e0e0f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 14 Jul 2023 11:48:06 +0200 Subject: [PATCH] tst_QParallelAnimationGroup: fix memleak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Setting the parent of a QObject to nullptr means the ex-parent no longer owns and deletes the object as its child, leaking it. Fix by creating a scope-guard to defer deletion until the tests have run. This is simpler than the alternatives: Putting it into unique_ptr would require a new variable name, or a larger refactoring of the function, because the `test` variable is being re-used for many different objects in the course of the function, most of which should not be deleted. Using QAutoPointer would drag in QtWidgetsPrivate, and the class is probably not available in all active branches. Finally, deleteLater() would require reliably returning to the event loop, which may not happen if the test is run in isolation. Bug exists since the dawn of the public history, and QScopeGuard is available in all active branches. Pick-to: 6.6 6.5 6.2 5.15 Change-Id: Ib4fcb44b0b68d4ccbcf5af144a18ffb378a72213 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Ivan Solovev --- .../qparallelanimationgroup/tst_qparallelanimationgroup.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/auto/corelib/animation/qparallelanimationgroup/tst_qparallelanimationgroup.cpp b/tests/auto/corelib/animation/qparallelanimationgroup/tst_qparallelanimationgroup.cpp index 3c5d9ed3a9..6c8f3558e6 100644 --- a/tests/auto/corelib/animation/qparallelanimationgroup/tst_qparallelanimationgroup.cpp +++ b/tests/auto/corelib/animation/qparallelanimationgroup/tst_qparallelanimationgroup.cpp @@ -7,6 +7,7 @@ #include #include +#include Q_DECLARE_METATYPE(QAbstractAnimation::State) @@ -928,6 +929,7 @@ void tst_QParallelAnimationGroup::autoAdd() test = static_cast(group.animationAt(0)); test->setParent(0); // remove the last one (with duration = 250) + const auto deleteParentlessObject = qScopeGuard([test] { delete test; }); QCOMPARE(test->group(), static_cast(0)); QCOMPARE(group.duration(), 0); }