From 0de7cafabc6d71cf3c129e7a67792d2a3ff1c864 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 31 Mar 2025 17:18:24 +0200 Subject: [PATCH] tst_QComboBox: fix memleak in getSetCheck() Contrary to what the code comment suggested, a QComboBox does not own the object passed in by setItemDelegate(). After a nullptr check, the function merely forwards to QComboBox::view()->setItemDelegate() and QAbstractItemView::setItemDelegate() documents very clearly that the item delegate ownership does not change. Consequently, the test function leaked the object. To fix, hold it in a unique_ptr and reset() at the end of the code block. I opted to not merely comment in the delete there, because that would still leak in case one of the QCOMPARE()s in-between failed. While it is not the purpose of the current work to make tests leak-free even on failure, it likewise makes little sense to not reap this small benefit now, albeit at the cost of a bit of git history mess-up, itself mediated by the fact that we're picking this all the way back, since this... Amends the start of the public history. Pick-to: 6.9 6.8 6.5 5.15 Change-Id: I1e604b795f5a12dc8829bf5913b88bc7c7110352 Reviewed-by: Axel Spoerl --- tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index 7936eb78e4e..41888727c06 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -322,13 +322,13 @@ void tst_QComboBox::getSetCheck() // QAbstractItemDelegate * QComboBox::itemDelegate() // void QComboBox::setItemDelegate(QAbstractItemDelegate *) - MyAbstractItemDelegate *var10 = new MyAbstractItemDelegate; - obj1.setItemDelegate(var10); - QCOMPARE(obj1.itemDelegate(), var10); + auto var10 = std::make_unique(); + obj1.setItemDelegate(var10.get()); + QCOMPARE(obj1.itemDelegate(), var10.get()); QTest::ignoreMessage(QtWarningMsg, "QComboBox::setItemDelegate: cannot set a 0 delegate"); obj1.setItemDelegate((QAbstractItemDelegate *)0); - QCOMPARE(obj1.itemDelegate(), var10); - // delete var10; // No delete, since QComboBox takes ownership + QCOMPARE(obj1.itemDelegate(), var10.get()); + var10.reset(); // QAbstractItemModel * QComboBox::model() // void QComboBox::setModel(QAbstractItemModel *)