@@ -180,12 +180,16 @@ void VirtualConnection::startProcess()
180180 << " type:" << simulatorType ();
181181
182182 m_process = new QProcess (this );
183+ connect (m_process, &QProcess::finished, this , &VirtualConnection::onProcessFinished);
184+ connect (m_process, &QProcess::errorOccurred, this , &VirtualConnection::onProcessErrorOccurred);
185+
183186 m_process->start (exePath, {m_server->serverName (), simulatorType ()});
184187
185188 if (!m_process->waitForStarted (3000 )) {
186189 qWarning () << qPrintable (QString (" [IO][%1]" ).arg (m_deviceName))
187190 << " Failed to start simulator process:" << m_process->errorString ();
188- delete m_process;
191+ m_process->disconnect (this );
192+ m_process->deleteLater ();
189193 m_process = nullptr ;
190194 setState (ConnectionState::Disconnected);
191195 }
@@ -197,14 +201,44 @@ void VirtualConnection::killProcess()
197201 return ;
198202 }
199203
200- qDebug () << qPrintable (QString (" [IO][%1]" ).arg (m_deviceName)) << " Killing simulator process..." ;
204+ // Disconnect signals first to avoid re-entering cleanup() via onProcessFinished().
205+ m_process->disconnect (this );
206+
207+ if (m_process->state () != QProcess::NotRunning) {
208+ qDebug () << qPrintable (QString (" [IO][%1]" ).arg (m_deviceName)) << " Killing simulator process..." ;
209+ m_process->kill ();
210+ if (!m_process->waitForFinished (2000 )) {
211+ qWarning () << qPrintable (QString (" [IO][%1]" ).arg (m_deviceName))
212+ << " Simulator process did not terminate within timeout." ;
213+ }
214+ } else {
215+ qDebug () << qPrintable (QString (" [IO][%1]" ).arg (m_deviceName)) << " Simulator process already stopped." ;
216+ }
201217
202- m_process->kill ();
203- m_process->waitForFinished (2000 );
204- delete m_process;
218+ m_process->deleteLater ();
205219 m_process = nullptr ;
206220}
207221
222+ void VirtualConnection::onProcessFinished (int exitCode, QProcess::ExitStatus status)
223+ {
224+ qDebug () << qPrintable (QString (" [IO][%1]" ).arg (m_deviceName))
225+ << " Simulator process finished. exitCode:" << exitCode
226+ << " status:" << (status == QProcess::NormalExit ? " NormalExit" : " CrashExit" );
227+
228+ cleanup ();
229+ }
230+
231+ void VirtualConnection::onProcessErrorOccurred (QProcess::ProcessError error)
232+ {
233+ qWarning () << qPrintable (QString (" [IO][%1]" ).arg (m_deviceName))
234+ << " Simulator process error:" << error
235+ << (m_process ? m_process->errorString () : QString ());
236+
237+ if (error == QProcess::Crashed || error == QProcess::FailedToStart) {
238+ cleanup ();
239+ }
240+ }
241+
208242#endif // VIRTUAL_SIMULATOR_PROCESS
209243
210244// Data transfer
0 commit comments