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 <axel.spoerl@qt.io>
This commit is contained in:
Marc Mutz 2025-03-31 17:18:24 +02:00
parent bf62a9762b
commit 0de7cafabc

View File

@ -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<MyAbstractItemDelegate>();
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 *)