Fix memory leak in tst_qtreewidget

Invalid inserts of items are tested. However, on an invalid insert
of QTreeWidgetItem into QTreeWidget, the QTreeWidget does not take
ownership of the to-be-inserted items. These items were leaked in
the past.
This patch introduces a scope-guard, so the items are not leaked.

Change-Id: Ib53ac02605c3a3c0bd1ae69bbd0876f860d08093
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Andreas Buhr 2021-07-08 10:06:07 +02:00 committed by Marc Mutz
parent 76e441c9ee
commit ec7989aa66

View File

@ -5,6 +5,7 @@
#include <QApplication>
#include <QHeaderView>
#include <QLineEdit>
#include <QScopeGuard>
#include <QScrollBar>
#include <QSignalSpy>
#include <QStyledItemDelegate>
@ -322,6 +323,7 @@ void tst_QTreeWidget::addTopLevelItem()
// invalid insert
{
tops.clear();
const auto sg = qScopeGuard([&] { qDeleteAll(std::exchange(tops, {})); });
for (int i = 0; i < 10; ++i)
tops << new TreeItem();
int count = tree.topLevelItemCount();