tst_QHostInfo: fix mem-leaks in threadSafetyAsynchronousAPI()

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 <marten.nordheim@qt.io>
(cherry picked from commit 69d767bec2)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-07-15 23:02:34 +02:00 committed by Qt Cherry-pick Bot
parent 7d55bf65fb
commit db1bf57a29

View File

@ -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<QThread*> threads;
QList<LookupReceiver*> 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);
}
}