From 686c44a69b13f6e884dd2b6d9991f4cd94597c5a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 28 Sep 2016 10:49:57 +0200 Subject: Plug remaining leaks in tests/auto/widgets/kernel The usual: - delete return values of QLayout::takeAt(), replaceWidget() - delete styles - delete top-level widgets - delete actions Either by naked delete, QScopedPointer or allocation on the stack instead of the heap. This fixes the remaining errors in GCC 6.1 Linux ASan runs of tests/auto/widgets/kernel. Change-Id: I8cc217be114b2e0edf34ad8d60dbf722f900bb7f Reviewed-by: Thiago Macieira --- tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'tests/auto/widgets/kernel/qformlayout') diff --git a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp index e1b494c9f1..5703d7e114 100644 --- a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp +++ b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp @@ -395,7 +395,8 @@ void tst_QFormLayout::setFormStyle() QCOMPARE(layout.rowWrapPolicy(), QFormLayout::DontWrapRows); #endif - widget.setStyle(QStyleFactory::create("windows")); + const QScopedPointer windowsStyle(QStyleFactory::create("windows")); + widget.setStyle(windowsStyle.data()); QCOMPARE(layout.labelAlignment(), Qt::AlignLeft); QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop)); @@ -406,14 +407,16 @@ void tst_QFormLayout::setFormStyle() this test is cross platform.. so create dummy styles that return all the right stylehints. */ - widget.setStyle(new DummyMacStyle()); + DummyMacStyle macStyle; + widget.setStyle(&macStyle); QCOMPARE(layout.labelAlignment(), Qt::AlignRight); QVERIFY(layout.formAlignment() == (Qt::AlignHCenter | Qt::AlignTop)); QCOMPARE(layout.fieldGrowthPolicy(), QFormLayout::FieldsStayAtSizeHint); QCOMPARE(layout.rowWrapPolicy(), QFormLayout::DontWrapRows); - widget.setStyle(new DummyQtopiaStyle()); + DummyQtopiaStyle qtopiaStyle; + widget.setStyle(&qtopiaStyle); QCOMPARE(layout.labelAlignment(), Qt::AlignRight); QVERIFY(layout.formAlignment() == (Qt::AlignLeft | Qt::AlignTop)); @@ -891,7 +894,7 @@ void tst_QFormLayout::takeAt() QCOMPARE(layout->count(), 7); for (int i = 6; i >= 0; --i) { - layout->takeAt(0); + delete layout->takeAt(0); QCOMPARE(layout->count(), i); } } @@ -983,7 +986,7 @@ void tst_QFormLayout::replaceWidget() QFormLayout::ItemRole role; // replace editor - layout->replaceWidget(edit1, edit3); + delete layout->replaceWidget(edit1, edit3); edit1->hide(); // Not strictly needed for the test, but for normal usage it is. QCOMPARE(layout->indexOf(edit1), -1); QCOMPARE(layout->indexOf(edit3), editIndex); @@ -994,7 +997,7 @@ void tst_QFormLayout::replaceWidget() QCOMPARE(rownum, 0); QCOMPARE(role, QFormLayout::FieldRole); - layout->replaceWidget(label1, label2); + delete layout->replaceWidget(label1, label2); label1->hide(); QCOMPARE(layout->indexOf(label1), -1); QCOMPARE(layout->indexOf(label2), labelIndex); -- cgit v1.2.3 From 080daae7cd7b75ad4a959e66ce91222e84f0bf31 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 28 Sep 2016 10:44:25 +0200 Subject: Plug new leaks in tst_QFormLayout The new takeRow() functions return a pair of pointers to QLayoutItems and, as the name particle 'take' suggests, releases ownership of these layout items. Which in turn means that the caller of the function is supposed to deal with them. This was not done here. To fix, write a RAII class that takes ownership of the returned layout items, deleting them when it goes out of scope or gets a new value assigned (only move special member functions are implemented, making the class move -only). Deleting the QLayoutItems is not so easy, though: QFormLayout has a special function for clearing the QLayoutItems out, so it appears that just calling their destructors is not going to fly (though I don't know off the top of the head why that should be a problem). Solve this, for now, by adding the layout items back into a temporary QFormLayout for destruction. Change-Id: If862989207b20f1e3f757c19ec9d498c4491184f Reviewed-by: Samuel Gaist Reviewed-by: Thiago Macieira --- .../widgets/kernel/qformlayout/tst_qformlayout.cpp | 44 ++++++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'tests/auto/widgets/kernel/qformlayout') diff --git a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp index b7ca5d21c7..10a1e9bf6b 100644 --- a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp +++ b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp @@ -51,6 +51,44 @@ static inline void setFrameless(QWidget *w) w->setWindowFlags(flags); } +struct QFormLayoutTakeRowResultHolder { + QFormLayoutTakeRowResultHolder(QFormLayout::TakeRowResult result) Q_DECL_NOTHROW + : labelItem(result.labelItem), + fieldItem(result.fieldItem) + { + } + ~QFormLayoutTakeRowResultHolder() + { + // re-use a QFormLayout to recursively reap the QLayoutItems: + QFormLayout disposer; + if (labelItem) + disposer.setItem(0, QFormLayout::LabelRole, labelItem); + if (fieldItem) + disposer.setItem(0, QFormLayout::FieldRole, fieldItem); + } + QFormLayoutTakeRowResultHolder(QFormLayoutTakeRowResultHolder &&other) Q_DECL_NOTHROW + : labelItem(other.labelItem), + fieldItem(other.fieldItem) + { + other.labelItem = nullptr; + other.fieldItem = nullptr; + } + QFormLayoutTakeRowResultHolder &operator=(QFormLayoutTakeRowResultHolder &&other) Q_DECL_NOTHROW + { + swap(other); + return *this; + } + + void swap(QFormLayoutTakeRowResultHolder &other) Q_DECL_NOTHROW + { + qSwap(labelItem, other.labelItem); + qSwap(fieldItem, other.fieldItem); + } + + QLayoutItem *labelItem; + QLayoutItem *fieldItem; +}; + class tst_QFormLayout : public QObject { Q_OBJECT @@ -814,7 +852,7 @@ void tst_QFormLayout::takeRow() QCOMPARE(layout->count(), 3); QCOMPARE(layout->rowCount(), 2); - QFormLayout::TakeRowResult result = layout->takeRow(1); + QFormLayoutTakeRowResultHolder result = layout->takeRow(1); QVERIFY(w2); QVERIFY(result.fieldItem); @@ -853,7 +891,7 @@ void tst_QFormLayout::takeRow_QWidget() QCOMPARE(layout->count(), 3); QCOMPARE(layout->rowCount(), 2); - QFormLayout::TakeRowResult result = layout->takeRow(w1); + QFormLayoutTakeRowResultHolder result = layout->takeRow(w1); QVERIFY(w1); QVERIFY(result.fieldItem); @@ -898,7 +936,7 @@ void tst_QFormLayout::takeRow_QLayout() QCOMPARE(layout->count(), 3); QCOMPARE(layout->rowCount(), 2); - QFormLayout::TakeRowResult result = layout->takeRow(l1); + QFormLayoutTakeRowResultHolder result = layout->takeRow(l1); QVERIFY(l1); QVERIFY(w1); -- cgit v1.2.3