Unsolved Executing QProcess in QThread: memory leak
-
@JonB @KroMignon
Agree with you, guys.
But it works...
I can reimplement to QProcess pointers of PingTester class... -
@sitesv said in Executing QProcess in QThread: memory leak:
But it works...
It don't, "it seems to work" would be the correct answer ;)
If you try to ping a not valid/accessible IP address, I am pretty sure it will not work.
If you want to do it sequentially, you have to be consistent in your choice:
foreach(auto ip, ip_list) { QProcess ping; ping.setProcessChannelMode(QProcess::MergedChannels); ping.start("/bin/ping", QStringList() << ip << "-c" << "1" << "-w" << "1"); ping.waitForFinished(5000); // wait up to 5 seconds QString output(ping.readAll()); if(output.contains("ttl",Qt::CaseInsensitive)){ success_count++; } }
-
@KroMignon said in Executing QProcess in QThread: memory leak:
If you try to ping a not valid/accessible IP address, I am pretty sure it will not work.
There is "-w 1" key. It helps with unaccessible IP.
-
@KroMignon
I would think doing it sequentially, withwaitForFinished()
, for his multiple IP addresses would be a poor way to do things. He has a number of IP addresses to check, these should be done in parallel.... -
@sitesv said in Executing QProcess in QThread: memory leak:
There is "-w 1" key. It helps with unaccessible IP.
AFAIK "-w 1" will wait up to 1 second.
I am sure you will got QProcess warnings about killing a process which is still running. -
@KroMignon said in Executing QProcess in QThread: memory leak:
AFAIK "-w 1" will wait up to 1 second.
...and QProcess will emit a "finished" signal. Why you are writing about "killing" process?
-
@sitesv said in Executing QProcess in QThread: memory leak:
...and QProcess will emit a "finished" signal. Why you are writing about "killing" process?
This is the way I would implement multiple pings in parallel:
int PingTester::doPing(const QStringList &ip_list) { // to avoid issues ;) if(ip_list.isEmpty()) return 0; int success_count = 0; int pingsToDo = ip_list.count(); QEventLoop l; QTimer timer; foreach(auto ip, ip_list) { auto ping = new QProcess(); ping->setProcessChannelMode(QProcess::MergedChannels); connect(timer, &QTimer::timeout, ping, &QProcess::kill); connect(ping, &QProcess::stateChanged, [&l, ping, &success_count, &pingsToDo](QProcess::ProcessState newState) { if(newState != QProcess::NotRunning) return; --pingsToDo; QString output(ping->readAll()); if(output.contains("ttl",Qt::CaseInsensitive)) success_count++; // free memory ping->deleteLater(); // exit loop when done. if(!pingsToDo) l.exit(); }); ping->start("/bin/ping", QStringList() << ip << "-c" << "1" << "-w" << "1"); } timer.start(1500); // kill process after 1.5 seconds if still running // wait all pings done l.exec(); return success_count; }
-
@jsulm There is a local QEventLoop...
@KroMignon I really don't understand why you are checking state changing of QProcess...
QProcess "Finished" method is not suitable?
And why kill a process? -
@sitesv said in Executing QProcess in QThread: memory leak:
QProcess "Finished" method is not suitable?
I found it easier to use
QProcess::stateChanged()
, because there is no overload of this slot, and I am not sure ifQProcess::finished(int, QProcess::ExitStatus)
is triggered if process is killed.And why kill a process?
This is a security, when calling an external application, many things can happen: execution right issues, application not exist, network issues and so on.
99.9% of time it will not be useful, but it will ensure QEventLoop will exit at the end! -
@KroMignon said in Executing QProcess in QThread: memory leak:
This is the way I would implement multiple pings in parallel:
This code doesn't work. Only one iteration.