Unsolved Executing QProcess in QThread: memory leak
-
@sitesv said in Executing QProcess in QThread: memory leak:
Are there any methods to do this correctly?
I don't say this is incorrect.
I depends what you want to achieve.
If you don't need an event queue, it works. If you are using signals/slots, this will not work.I do not know what is the purpose of this code extract, so maybe it is the best way to do.
-
@KroMignon said in Executing QProcess in QThread: memory leak:
If you don't need an event queue, it works. If you are using signals/slots, this will not work.
I don't know, but from this qthread in my main app I emit signal... and it is emitted... slot is from the main thread. QThread starts by start() method.
The problem for me is memory leak only. -
@JonB said in Executing QProcess in QThread: memory leak:
In addition, from @KroMignon no QEventLoop also means no deleting/disposing/freeing of Qt objects.
QCoreApplication::processEvents would help?
@JonB said in Executing QProcess in QThread: memory leak:
is wrong: -c & 1 are separate arguments, it ought to be << "-c" << "1". You are lucky it works as one argument.
Thank you! Fixed.
-
@sitesv said in Executing QProcess in QThread: memory leak:
I don't know, but from this qthread in my main app I emit signal... and it is emitted... slot is from the main thread. QThread starts by start() method.
The problem for me is memory leak only.For my comprehension of
QObject
andQThread
(cf Threads and QObjects), your implementation is not really Qt friendly.If I right understand what you want to achieve: you want to run "ping" a regular interval and be informed when ping has failed.
To do this, I would create a QObject class which handles the ping requests and eventually moves this class instance to a dedicated thread to avoid lock on main thread.
Something like:
class PingTester : public QObject { Q_OBJECT public: explicit PingTester(QObject* parent = nullptr): QObject(parent), m_timer(new QTimer(this)) { m_timer->setInterval(30*1000); // per default 30 seconds m_timer->setSingleShot(false); connect(m_timer, &QTimer::timeout, this, &PingTester::doPing); } void setInterval(int interval) { m_timer->setInterval(interval); } public slots: void start() { if(QThread::currentThrea() != thread()) { QTimer::singleShot(this, 0, &PingTester::start); return; } if(!m_timer->isActive()) m_timer->start(); } void stop() { if(QThread::currentThrea() != thread()) { QTimer::singleShot(this, 0, &PingTester::stop); return; } if(m_timer->isActive()) m_timer->stop(); } private slots: void doPing(); signals: void pingFailure(); void pingSuccess(); private: QTimer* m_timer; };
-
Hi,
Can you explain why exactly do you need that ping ?
-
@SGaist
ping needs to check the availability of pc from the internal network. I thought that this is the simplest way to do this. -
But why a thread then?
-
@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? -
@Christian-Ehrlicher said in Executing QProcess in QThread: memory leak:
But why a thread then?
I need to check availability constantly during the program. I thought the QThread is a good way to do this...
-
QProcess is asynchronous.
-
@Christian-Ehrlicher said in Executing QProcess in QThread: memory leak:
QProcess is asynchronous.
"waitForFinished" is not help?
-
@sitesv said in Executing QProcess in QThread: memory leak:
"waitForFinished" is not help?
Why do you want to make a asynchronous object blocking? Please read the QProcess docs and use the proper signals.
-
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.
-
@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. -
@JonB
I will try it. -
@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.
-
@SGaist code updated
-
@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(); }