diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-09-28 10:44:25 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-10-21 21:49:21 +0000 |
commit | 080daae7cd7b75ad4a959e66ce91222e84f0bf31 (patch) | |
tree | ff01810214523ff63d5a6b2a335fbfd0c9e6828c /tests/auto/widgets/kernel/qformlayout | |
parent | 512934f7e70592ed06a790fcb46dde1e435b488e (diff) |
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 <samuel.gaist@edeltech.ch>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/widgets/kernel/qformlayout')
-rw-r--r-- | tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp | 44 |
1 files changed, 41 insertions, 3 deletions
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); |