|author||David Faure <email@example.com>||2017-08-25 10:29:33 +0200|
|committer||David Faure <firstname.lastname@example.org>||2017-10-06 10:43:03 +0000|
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ø <email@example.com>
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
@@ -901,6 +901,10 @@ void TestMethods::invokeTestOnData(int index) const
+ // 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.