From 23d0c77ce86927963ea2b0a31564cfd5189a7539 Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 25 Aug 2017 10:29:33 +0200 Subject: QTestLib: flush DeferredDelete events between every test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ø --- src/testlib/qtestcase.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/testlib') 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) QTestResult::finishedCurrentTestDataCleanup(); -- cgit v1.2.3