path: root/src/testlib
diff options
authorDavid Faure <>2017-08-25 10:29:33 +0200
committerDavid Faure <>2017-10-06 10:43:03 +0000
commit23d0c77ce86927963ea2b0a31564cfd5189a7539 (patch)
tree74239fc0b8423e6d17e8645e5051618b09c5dc4c /src/testlib
parentcccede8fc75d5bdcfc7be3b4390bca52f0f2093f (diff)
QTestLib: flush DeferredDelete events between every test
This makes qtestlib behave more like real Qt applications with an event loop, where exiting the application does indeed flush deleteLaters. And since every test method is supposed to be independent from other test methods, we should even cleanup between tests. For "app less" tests, no flushing happens. Real life use cases: * A unittest for some code (e.g. KIO job) which uses a socket, runs an event loop until the socket communication is done, and ends. However slotDisconnected() does, as recommended, socket->deleteLater(). So the test finishes before the socket has a chance to actually get deleted, and memory leak tools flag a memory leak, which doesn't actually happen outside the unittest. * Deleting a QWebEngineView with deleteLater is even worse than a memleak, it leads to an assert (from a global object destructor) in QtWebEngine if the view deletion hasn't actually been processed. Change-Id: I18fc822fd26988a0fa4e75313c1947fcaa7d9e56 Reviewed-by: Tor Arne Vestbø <>
Diffstat (limited to 'src/testlib')
1 files changed, 4 insertions, 0 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index eeaa30e9a0..8f8bb83f11 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -901,6 +901,10 @@ void TestMethods::invokeTestOnData(int index) const
if (m_cleanupMethod.isValid())
m_cleanupMethod.invoke(QTest::currentTestObject, Qt::DirectConnection);
+ // Process any deleteLater(), like event-loop based apps would do. Fixes memleak reports.
+ if (QCoreApplication::instance())
+ QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
// If the test isn't a benchmark, finalize the result after cleanup() has finished.
if (!isBenchmark)