Executing QProcess in QThread: memory leak
-
The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?
@SGaist said in Executing QProcess in QThread: memory leak:
The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?
I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.
-
@SGaist said in Executing QProcess in QThread: memory leak:
The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?
I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.
@sitesv
As @Christian-Ehrlicher has said. You are using this thread to run aQProcess
, and you callwaitForFinished()
on it. You would be better just runningQProcess
asynchronously from your main thread, and acting on signals. Then perhaps any thread memory issue will go away. -
@sitesv
As @Christian-Ehrlicher has said. You are using this thread to run aQProcess
, and you callwaitForFinished()
on it. You would be better just runningQProcess
asynchronously from your main thread, and acting on signals. Then perhaps any thread memory issue will go away. -
@SGaist said in Executing QProcess in QThread: memory leak:
The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?
I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.
@sitesv said in Executing QProcess in QThread: memory leak:
@SGaist said in Executing QProcess in QThread: memory leak:
The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?
I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.
Well, do not skip that kind of details when asking questions, it can hide the issue you are asking help for and it does not allow people to understand what is really happening.
-
@sitesv said in Executing QProcess in QThread: memory leak:
@SGaist said in Executing QProcess in QThread: memory leak:
The thing is: you just ping in the void, you do not even do anything with that information so why even ping ?
I just skipped this code, it is unimportant... It receives an answer, analyzes it, and emits a signal to the main thread. And this is works.
Well, do not skip that kind of details when asking questions, it can hide the issue you are asking help for and it does not allow people to understand what is really happening.
-
@KroMignon Thank you for your variant.
I have some Qs;
Why you are using QTimer for this purpose?
doPing() is represents my code with using QProcess?
This class should create in the main thread?@sitesv said in Executing QProcess in QThread: memory leak:
Why you are using QTimer for this purpose?
I use
QTimer
here to make a passive wait. When the interval is reached, theQTimer
fires thetimeout()
event. This will not lock the event queue.QThread::msleep()
is an active wait ==> CPU usage for nothing and no signals/slots handling possible for the used thread!doPing() is represents my code with using QProcess?
Yes, but I would not use a pointer for the QProcess.
This class should create in the main thread?
As you like. The
QTimer
instance has this as parent, moving the PingTester instance to another thread, will also move theQTimer
instance to the same thread. -
@sitesv For example a possible implementation which will not lock the event loop could be:
void PingTester::doPing() { QProcess ping; QEventLoop l; // used for passive wait until process finished ping.setProcessChannelMode(QProcess::MergedChannels); connect(&ping, &QProcess::finished, [this, &l, &ping]() { QString output(ping.readAll()); if(output.contains("ttl",Qt::CaseInsensitive)) emit pingSuccess(); else emit pingFailure(); // exit event loop on process end l.exit(); }); // starting ping request ping.start("/bin/ping", QStringList() << "127.0.0.1" << "-c" << "1"); // wait until ping finished l.exec(); // start next ping (timer should be configured as single shot) m_timer->start(); }
-
@sitesv For example a possible implementation which will not lock the event loop could be:
void PingTester::doPing() { QProcess ping; QEventLoop l; // used for passive wait until process finished ping.setProcessChannelMode(QProcess::MergedChannels); connect(&ping, &QProcess::finished, [this, &l, &ping]() { QString output(ping.readAll()); if(output.contains("ttl",Qt::CaseInsensitive)) emit pingSuccess(); else emit pingFailure(); // exit event loop on process end l.exit(); }); // starting ping request ping.start("/bin/ping", QStringList() << "127.0.0.1" << "-c" << "1"); // wait until ping finished l.exec(); // start next ping (timer should be configured as single shot) m_timer->start(); }
@KroMignon Thank you for the new experience.
There is a problem with 'connect' -
@KroMignon Thank you for the new experience.
There is a problem with 'connect' -
@sitesv said in Executing QProcess in QThread: memory leak:
There is a problem with 'connect'
Can you detail more the problem?
@KroMignon said in Executing QProcess in QThread: memory leak:
Can you detail more the problem?
pingtester.cpp:9:5: error: no matching member function for call to 'connect'
qobject.h:481:41: note: candidate function not viable: no overload of 'finished' matching 'const char *' for 2nd argument
qobject.h:274:13: note: candidate template ignored: couldn't infer template argument 'Func1'
qobject.h:314:13: note: candidate template ignored: couldn't infer template argument 'Func1'
qobject.h:242:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
qobject.h:283:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
qobject.h:322:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
qobject.h:222:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
qobject.h:225:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided -
@KroMignon said in Executing QProcess in QThread: memory leak:
Can you detail more the problem?
pingtester.cpp:9:5: error: no matching member function for call to 'connect'
qobject.h:481:41: note: candidate function not viable: no overload of 'finished' matching 'const char *' for 2nd argument
qobject.h:274:13: note: candidate template ignored: couldn't infer template argument 'Func1'
qobject.h:314:13: note: candidate template ignored: couldn't infer template argument 'Func1'
qobject.h:242:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
qobject.h:283:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
qobject.h:322:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
qobject.h:222:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
qobject.h:225:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided@sitesv Oh, my bad, QProcess::finished() has an overload (https://doc.qt.io/qt-5/qprocess.html#finished)
Should be changed to:
connect(&ping, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [this, &l, &ping]() { QString output(ping.readAll()); if(output.contains("ttl",Qt::CaseInsensitive)) emit pingSuccess(); else emit pingFailure(); // exit event loop on process end l.exit(); });
-
@KroMignon
Very interesting realization. I will study it.
Thank you again! -
@KroMignon
Hi!
Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((. -
@KroMignon
Hi!
Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((.@sitesv said in Executing QProcess in QThread: memory leak:
Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((.
Yes, it is possible.
I have give you a basic implementation example.
You just have to complete it.If you want to learn lambda, search on internet, there are many explanations. It is not so hard to understand.
My favorite is https://blog.feabhas.com/2014/03/demystifying-c-lambdas/ -
@sitesv said in Executing QProcess in QThread: memory leak:
Is it possible to make a checking ping of a list of addresses for each timer interval? I haven't used a lambda yet in my practice ((.
Yes, it is possible.
I have give you a basic implementation example.
You just have to complete it.If you want to learn lambda, search on internet, there are many explanations. It is not so hard to understand.
My favorite is https://blog.feabhas.com/2014/03/demystifying-c-lambdas/@KroMignon said in Executing QProcess in QThread: memory leak:
Yes, it is possible.
I have tried to do something like this. But is execute too slow.
QStringList ip_list = {"192.168.0.1", "192.168.0.2"}; int success_count = 0; foreach(auto ip, ip_list) { QProcess ping; QEventLoop l; ping.setProcessChannelMode(QProcess::MergedChannels); connect(&ping, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [&l, &ping, &success_count]() { QString output(ping.readAll()); if(output.contains("ttl",Qt::CaseInsensitive)) success_count++; l.exit(); }); ping.start("/bin/ping", QStringList() << ip << "-c" << "1"); l.exec(); } if(success_count == ip_list.count()) emit setStatus(true); else emit setStatus(false); m_timer->start();
-
@KroMignon said in Executing QProcess in QThread: memory leak:
Yes, it is possible.
I have tried to do something like this. But is execute too slow.
QStringList ip_list = {"192.168.0.1", "192.168.0.2"}; int success_count = 0; foreach(auto ip, ip_list) { QProcess ping; QEventLoop l; ping.setProcessChannelMode(QProcess::MergedChannels); connect(&ping, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [&l, &ping, &success_count]() { QString output(ping.readAll()); if(output.contains("ttl",Qt::CaseInsensitive)) success_count++; l.exit(); }); ping.start("/bin/ping", QStringList() << ip << "-c" << "1"); l.exec(); } if(success_count == ip_list.count()) emit setStatus(true); else emit setStatus(false); m_timer->start();
This post is deleted! -
UPD:
I found the cause of this situation. Ip's are not available. How to set a timeout for the process execution in this case?@sitesv Why do you actually use a local event loop? You can implement this functionality without blocking your app with a local event loop.
Also, you terminate that event loop already in the first lambda call (when the first process finishes)... -
@sitesv Why do you actually use a local event loop? You can implement this functionality without blocking your app with a local event loop.
Also, you terminate that event loop already in the first lambda call (when the first process finishes)... -
@sitesv You know how many processes you started. So, count how many processes already finished (inside the lambda). And as soon as all processes finished you can check success_count and emit the signal. All this can be done inside lambda.