Don't instantiate QTimer and QEventLoop for every QtcProcess

These two objects are only used when QtcProcess::runBlocking()
with m_processUserEvents option set is called, otherwise
they are unused. Create them only when they are needed.

This also solves the issue that these two QObjects were not
moved together with QtcProcess when it was moved to another
thread.

Change-Id: I7de6a92a51fc249184fc5811dac7a7bbf05d351f
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2021-10-20 17:38:11 +02:00
parent 0f552a4f28
commit 4b6b9f4dec

View File

@@ -493,8 +493,6 @@ public:
this, &QtcProcessPrivate::handleReadyReadStandardOutput);
connect(m_process, &ProcessInterface::readyReadStandardError,
this, &QtcProcessPrivate::handleReadyReadStandardError);
connect(&m_timer, &QTimer::timeout, this, &QtcProcessPrivate::slotTimeout);
m_timer.setInterval(1000);
}
void handleReadyReadStandardOutput()
@@ -552,8 +550,7 @@ public:
QtcProcess::Result interpretExitCode(int exitCode);
QTextCodec *m_codec = QTextCodec::codecForLocale();
QTimer m_timer;
QEventLoop m_eventLoop;
QEventLoop *m_eventLoop = nullptr;
QtcProcess::Result m_result = QtcProcess::StartFailed;
QProcess::ExitStatus m_exitStatus = QProcess::NormalExit;
ChannelBuffer m_stdOut;
@@ -1408,16 +1405,23 @@ void QtcProcess::runBlocking()
// executable cannot be found in the path. Do not start the
// event loop in that case.
if (!d->m_startFailure) {
d->m_timer.start();
QTimer timer(this);
connect(&timer, &QTimer::timeout, d, &QtcProcessPrivate::slotTimeout);
timer.setInterval(1000);
timer.start();
#ifdef QT_GUI_LIB
if (isGuiThread())
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
d->m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
QEventLoop eventLoop(this);
QTC_ASSERT(!d->m_eventLoop, return);
d->m_eventLoop = &eventLoop;
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
d->m_eventLoop = nullptr;
d->m_stdOut.append(d->m_process->readAllStandardOutput());
d->m_stdErr.append(d->m_process->readAllStandardError());
d->m_timer.stop();
timer.stop();
#ifdef QT_GUI_LIB
if (isGuiThread())
QApplication::restoreOverrideCursor();
@@ -1510,7 +1514,8 @@ void QtcProcessPrivate::slotFinished(int exitCode, QProcess::ExitStatus status)
m_result = QtcProcess::TerminatedAbnormally;
break;
}
m_eventLoop.quit();
if (m_eventLoop)
m_eventLoop->quit();
m_stdOut.handleRest();
m_stdErr.handleRest();
@@ -1527,7 +1532,8 @@ void QtcProcessPrivate::slotError(QProcess::ProcessError error)
if (m_result != QtcProcess::Hang)
m_result = QtcProcess::StartFailed;
m_startFailure = true;
m_eventLoop.quit();
if (m_eventLoop)
m_eventLoop->quit();
emit q->errorOccurred(error);
}