From 8bf4c0b4a1a5516f88ec3c39767dd4dcebf114c4 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Mon, 20 Jan 2025 15:09:11 +0100 Subject: [PATCH] Python: avoid accessing deleted extra compiler It looks like we delete extra compilers without properly unsetting them in the python language client. Connect to the destroyed signal of the extra compilers and ensure that the compiler is untracked in the client with an assert if it was not properly untracked before the destruction. Fixes: QTCREATORBUG-32362 Change-Id: I53dbf2877d74420424f5a590d4fc0eeb3df469ef Reviewed-by: Christian Stenger --- src/plugins/python/pythonlanguageclient.cpp | 30 ++++++++++++++------- src/plugins/python/pythonlanguageclient.h | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/plugins/python/pythonlanguageclient.cpp b/src/plugins/python/pythonlanguageclient.cpp index 3f8c1b94bd3..5a77ca7991e 100644 --- a/src/plugins/python/pythonlanguageclient.cpp +++ b/src/plugins/python/pythonlanguageclient.cpp @@ -226,7 +226,7 @@ void PyLSClient::openDocument(TextEditor::TextDocument *document) void PyLSClient::projectClosed(ProjectExplorer::Project *project) { for (ProjectExplorer::ExtraCompiler *compiler : m_extraCompilers.value(project)) - closeExtraCompiler(compiler); + closeExtraCompiler(compiler, compiler->targets().first()); Client::projectClosed(project); } @@ -239,12 +239,23 @@ void PyLSClient::updateExtraCompilers(ProjectExplorer::Project *project, int index = oldCompilers.indexOf(extraCompiler); if (index < 0) { m_extraCompilers[project] << extraCompiler; - connect(extraCompiler, - &ExtraCompiler::contentsChanged, - this, - [this, extraCompiler](const FilePath &file) { - updateExtraCompilerContents(extraCompiler, file); - }); + connect( + extraCompiler, + &ExtraCompiler::contentsChanged, + this, + [this, extraCompiler](const FilePath &file) { + updateExtraCompilerContents(extraCompiler, file); + }); + connect( + extraCompiler, + &QObject::destroyed, + this, + [this, extraCompiler, file = extraCompiler->targets().constFirst()]() { + for (QList &extraCompilers : m_extraCompilers) + QTC_CHECK(extraCompilers.removeAll(extraCompiler) == 0); + closeExtraCompiler(extraCompiler, file); + }); + if (extraCompiler->isDirty()) extraCompiler->compileFile(); } else { @@ -252,7 +263,7 @@ void PyLSClient::updateExtraCompilers(ProjectExplorer::Project *project, } } for (ProjectExplorer::ExtraCompiler *compiler : oldCompilers) - closeExtraCompiler(compiler); + closeExtraCompiler(compiler, compiler->targets().first()); } void PyLSClient::updateExtraCompilerContents(ExtraCompiler *compiler, const FilePath &file) @@ -262,9 +273,8 @@ void PyLSClient::updateExtraCompilerContents(ExtraCompiler *compiler, const File target.writeFileContents(compiler->content(file)); } -void PyLSClient::closeExtraCompiler(ProjectExplorer::ExtraCompiler *compiler) +void PyLSClient::closeExtraCompiler(ProjectExplorer::ExtraCompiler *compiler, const FilePath &file) { - const FilePath file = compiler->targets().constFirst(); m_extraCompilerOutputDir.pathAppended(file.fileName()).removeFile(); compiler->disconnect(this); } diff --git a/src/plugins/python/pythonlanguageclient.h b/src/plugins/python/pythonlanguageclient.h index 4c740dc7a96..33874ea4498 100644 --- a/src/plugins/python/pythonlanguageclient.h +++ b/src/plugins/python/pythonlanguageclient.h @@ -37,7 +37,7 @@ private: void updateExtraCompilerContents(ProjectExplorer::ExtraCompiler *compiler, const Utils::FilePath &file); void closeExtraDoc(const Utils::FilePath &file); - void closeExtraCompiler(ProjectExplorer::ExtraCompiler *compiler); + void closeExtraCompiler(ProjectExplorer::ExtraCompiler *compiler, const Utils::FilePath &file); Utils::FilePaths m_extraWorkspaceDirs; Utils::FilePath m_extraCompilerOutputDir;