From 4b6b9f4dec5890815cecaff4702aacc6fe209797 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Wed, 20 Oct 2021 17:38:11 +0200 Subject: [PATCH] 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 Reviewed-by: hjk --- src/libs/utils/qtcprocess.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/libs/utils/qtcprocess.cpp b/src/libs/utils/qtcprocess.cpp index f4b715eece6..0975b3fe220 100644 --- a/src/libs/utils/qtcprocess.cpp +++ b/src/libs/utils/qtcprocess.cpp @@ -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); }