From db1bf57a292e7df0520833a29c7cd05e5dbf0804 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 15 Jul 2023 23:02:34 +0200 Subject: [PATCH] tst_QHostInfo: fix mem-leaks in threadSafetyAsynchronousAPI() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allocate participating threads and objects on the stack, not the heap. As a drive-by, port from QList to C arrays (never use a dynamically-sized container for statically-sized data™). Code predates the public history, all active branches are affected. Change-Id: If8def658c1c7b505074938d637e78ad2d1f9fd57 Reviewed-by: Mårten Nordheim (cherry picked from commit 69d767bec265587a5645a08f14bb7e7540f01867) Reviewed-by: Qt Cherry-pick Bot --- .../kernel/qhostinfo/tst_qhostinfo.cpp | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp index e6704c36236..195cf972d9e 100644 --- a/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp +++ b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp @@ -43,7 +43,6 @@ #define TEST_DOMAIN ".test.qt-project.org" - class tst_QHostInfo : public QObject { Q_OBJECT @@ -552,24 +551,22 @@ void tst_QHostInfo::threadSafetyAsynchronousAPI() { const int nattempts = 10; const int lookupsperthread = 10; - QList threads; - QList receivers; + QThread threads[nattempts]; + LookupReceiver receivers[nattempts]; for (int i = 0; i < nattempts; ++i) { - QThread* thread = new QThread; - LookupReceiver* receiver = new LookupReceiver; + QThread *thread = &threads[i]; + LookupReceiver *receiver = &receivers[i]; receiver->numrequests = lookupsperthread; - receivers.append(receiver); receiver->moveToThread(thread); connect(thread, SIGNAL(started()), receiver, SLOT(start())); thread->start(); - threads.append(thread); } - for (int k = threads.size() - 1; k >= 0; --k) - QVERIFY(threads.at(k)->wait(60000)); - foreach (LookupReceiver* receiver, receivers) { - QCOMPARE(receiver->result.error(), QHostInfo::NoError); - QCOMPARE(receiver->result.addresses().at(0).toString(), QString("192.0.2.1")); - QCOMPARE(receiver->numrequests, 0); + for (int k = nattempts - 1; k >= 0; --k) + QVERIFY(threads[k].wait(60000)); + for (LookupReceiver &receiver : receivers) { + QCOMPARE(receiver.result.error(), QHostInfo::NoError); + QCOMPARE(receiver.result.addresses().at(0).toString(), QString("192.0.2.1")); + QCOMPARE(receiver.numrequests, 0); } }