LanguageClient: fix possible crash on shutdown

Iterate on a copy of managerInstance->m_clients
when calling shutdownClient() or deleteClient(),
since both methods may potentially modify m_clients
list and thus invalidate outer iterators.

Surprisingly, this patch also fixes the leak
of RunControl and RunWorker instances on shutdown.

Task-number: QTCREATORBUG-25709
Fixes: QTCREATORBUG-26847
Change-Id: Ib34d913a6ae0b235631d3d619bddaf4e08b4aec2
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Jarek Kobus
2022-01-17 10:36:59 +01:00
parent f9c21253c0
commit e1f45507c5

View File

@@ -253,10 +253,12 @@ void LanguageClientManager::shutdown()
return; return;
qCDebug(Log) << "shutdown manager"; qCDebug(Log) << "shutdown manager";
managerInstance->m_shuttingDown = true; managerInstance->m_shuttingDown = true;
for (Client *client : qAsConst(managerInstance->m_clients)) const auto clients = managerInstance->clients();
for (Client *client : clients)
shutdownClient(client); shutdownClient(client);
QTimer::singleShot(3000, managerInstance, [](){ QTimer::singleShot(3000, managerInstance, [] {
for (Client *client : qAsConst(managerInstance->m_clients)) const auto clients = managerInstance->clients();
for (Client *client : clients)
deleteClient(client); deleteClient(client);
emit managerInstance->shutdownFinished(); emit managerInstance->shutdownFinished();
}); });