From 6ea626a32fe070847629b6715c2a253717ff7412 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 22 Sep 2016 23:04:37 +0200 Subject: QObject test: check that throwing from a child's constructor works A check "just in case" -- we don't want leaks nor crashes due to double deletions, and so on. Change-Id: I24f1a486f0d438595bbe352ab780b07c5d53acbd Reviewed-by: Olivier Goffart (Woboq GmbH) --- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 76 +++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'tests/auto/corelib/kernel/qobject/tst_qobject.cpp') diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 46889225eb..4417aa2353 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -6333,6 +6333,29 @@ signals: CountedStruct mySignal(const CountedStruct &s1, CountedStruct s2); }; +class CountedExceptionThrower : public QObject +{ + Q_OBJECT + +public: + explicit CountedExceptionThrower(bool throwException, QObject *parent = Q_NULLPTR) + : QObject(parent) + { + if (throwException) + throw ObjectException(); + ++counter; + } + + ~CountedExceptionThrower() + { + --counter; + } + + static int counter; +}; + +int CountedExceptionThrower::counter = 0; + void tst_QObject::exceptions() { #ifndef QT_NO_EXCEPTIONS @@ -6394,6 +6417,59 @@ void tst_QObject::exceptions() } QCOMPARE(countedStructObjectsCount, 0); + // Child object reaping in case of exceptions thrown by constructors + { + QCOMPARE(CountedExceptionThrower::counter, 0); + + try { + class ParentObject : public QObject { + public: + explicit ParentObject(QObject *parent = Q_NULLPTR) + : QObject(parent) + { + new CountedExceptionThrower(false, this); + new CountedExceptionThrower(false, this); + new CountedExceptionThrower(true, this); // throws + } + }; + + ParentObject p; + QFAIL("Exception not thrown"); + } catch (const ObjectException &) { + } catch (...) { + QFAIL("Wrong exception thrown"); + } + + QCOMPARE(CountedExceptionThrower::counter, 0); + + try { + QObject o; + new CountedExceptionThrower(false, &o); + new CountedExceptionThrower(false, &o); + new CountedExceptionThrower(true, &o); // throws + + QFAIL("Exception not thrown"); + } catch (const ObjectException &) { + } catch (...) { + QFAIL("Wrong exception thrown"); + } + + QCOMPARE(CountedExceptionThrower::counter, 0); + + try { + QObject o; + CountedExceptionThrower c1(false, &o); + CountedExceptionThrower c2(false, &o); + CountedExceptionThrower c3(true, &o); // throws + + QFAIL("Exception not thrown"); + } catch (const ObjectException &) { + } catch (...) { + QFAIL("Wrong exception thrown"); + } + + QCOMPARE(CountedExceptionThrower::counter, 0); + } #else QSKIP("Needs exceptions"); -- cgit v1.2.3