From 8758c3c8dd75d13868b844867c149d8c8f7029e9 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 12 Jul 2024 14:53:34 +0200 Subject: [PATCH] Editors: Fix crash after triggering dropdown menu after closing editor The context menu kept a handle on the editor and document, and these could be deleted behind its back. Use QPointers to actually check. Fixes: QTCREATORBUG-31232 Change-Id: I613abbe6dc0fbac60c2e527f715a7b8c2f083893 Reviewed-by: Christian Stenger --- .../editormanager/editormanager.cpp | 30 ++++++++++--------- .../editormanager/editormanager_p.h | 3 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index c8ef2a4f1ab..55c3dcdd3b3 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -539,12 +539,12 @@ void EditorManagerPrivate::init() this, &EditorManagerPrivate::closeAllEditorsExceptVisible); connect(m_openGraphicalShellContextAction, &QAction::triggered, this, [this] { - if (!m_contextMenuEntry || m_contextMenuEntry->filePath().isEmpty()) + if (!m_contextMenuDocument || m_contextMenuEntry->filePath().isEmpty()) return; FileUtils::showInGraphicalShell(ICore::dialogParent(), m_contextMenuEntry->filePath()); }); connect(m_showInFileSystemViewContextAction, &QAction::triggered, this, [this] { - if (!m_contextMenuEntry || m_contextMenuEntry->filePath().isEmpty()) + if (!m_contextMenuDocument || m_contextMenuEntry->filePath().isEmpty()) return; FileUtils::showInFileSystemView(m_contextMenuEntry->filePath()); }); @@ -552,7 +552,7 @@ void EditorManagerPrivate::init() connect(m_findInDirectoryAction, &QAction::triggered, this, &EditorManagerPrivate::findInDirectory); connect(m_filePropertiesAction, &QAction::triggered, this, [this] { - if (!m_contextMenuEntry || m_contextMenuEntry->filePath().isEmpty()) + if (!m_contextMenuDocument || m_contextMenuEntry->filePath().isEmpty()) return; DocumentManager::showFilePropertiesDialog(m_contextMenuEntry->filePath()); }); @@ -2402,14 +2402,14 @@ void EditorManagerPrivate::handleContextChange(const QList &context) void EditorManagerPrivate::copyFilePathFromContextMenu() { - if (!d->m_contextMenuEntry) + if (!d->m_contextMenuDocument) return; setClipboardAndSelection(d->m_contextMenuEntry->filePath().toUserOutput()); } void EditorManagerPrivate::copyLocationFromContextMenu() { - if (!d->m_contextMenuEntry) + if (!d->m_contextMenuDocument) return; const QString text = d->m_contextMenuEntry->filePath().toUserOutput() + QLatin1Char(':') + m_copyLocationContextAction->data().toString(); @@ -2418,28 +2418,28 @@ void EditorManagerPrivate::copyLocationFromContextMenu() void EditorManagerPrivate::copyFileNameFromContextMenu() { - if (!d->m_contextMenuEntry) + if (!d->m_contextMenuDocument) return; setClipboardAndSelection(d->m_contextMenuEntry->filePath().fileName()); } void EditorManagerPrivate::saveDocumentFromContextMenu() { - IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : nullptr; + IDocument *document = d->m_contextMenuDocument.get(); if (document) saveDocument(document); } void EditorManagerPrivate::saveDocumentAsFromContextMenu() { - IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : nullptr; + IDocument *document = d->m_contextMenuDocument.get(); if (document) saveDocumentAs(document); } void EditorManagerPrivate::revertToSavedFromContextMenu() { - IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : nullptr; + IDocument *document = d->m_contextMenuDocument.get(); if (document) revertToSaved(document); } @@ -2449,7 +2449,7 @@ void EditorManagerPrivate::closeEditorFromContextMenu() if (d->m_contextMenuEditor) { closeEditorOrDocument(d->m_contextMenuEditor); } else { - IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : nullptr; + IDocument *document = d->m_contextMenuDocument.get(); if (document) EditorManager::closeDocuments({document}); } @@ -2457,7 +2457,7 @@ void EditorManagerPrivate::closeEditorFromContextMenu() void EditorManagerPrivate::closeOtherDocumentsFromContextMenu() { - IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : nullptr; + IDocument *document = d->m_contextMenuDocument.get(); EditorManager::closeOtherDocuments(document); } @@ -2613,14 +2613,14 @@ void EditorManagerPrivate::autoSuspendDocuments() void EditorManagerPrivate::openTerminal() { - if (!d->m_contextMenuEntry || d->m_contextMenuEntry->filePath().isEmpty()) + if (!d->m_contextMenuDocument || d->m_contextMenuEntry->filePath().isEmpty()) return; FileUtils::openTerminal(d->m_contextMenuEntry->filePath().parentDir(), {}); } void EditorManagerPrivate::findInDirectory() { - if (!d->m_contextMenuEntry || d->m_contextMenuEntry->filePath().isEmpty()) + if (!d->m_contextMenuDocument || d->m_contextMenuEntry->filePath().isEmpty()) return; const FilePath path = d->m_contextMenuEntry->filePath(); emit m_instance->findOnFileSystemRequest( @@ -2629,7 +2629,7 @@ void EditorManagerPrivate::findInDirectory() void EditorManagerPrivate::togglePinned() { - if (!d->m_contextMenuEntry || d->m_contextMenuEntry->filePath().isEmpty()) + if (!d->m_contextMenuDocument || d->m_contextMenuEntry->filePath().isEmpty()) return; const bool currentlyPinned = d->m_contextMenuEntry->pinned; @@ -2832,6 +2832,7 @@ void EditorManager::addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentMod { QTC_ASSERT(contextMenu, return); d->m_contextMenuEntry = entry; + d->m_contextMenuDocument = entry ? entry->document : nullptr; d->m_contextMenuEditor = editor; const FilePath filePath = entry ? entry->filePath() : FilePath(); @@ -2911,6 +2912,7 @@ void EditorManager::addNativeDirAndOpenWithActions(QMenu *contextMenu, DocumentM { QTC_ASSERT(contextMenu, return); d->m_contextMenuEntry = entry; + d->m_contextMenuDocument = entry ? entry->document : nullptr; bool enabled = entry && !entry->filePath().isEmpty(); d->m_openGraphicalShellContextAction->setEnabled(enabled); d->m_showInFileSystemViewContextAction->setEnabled(enabled); diff --git a/src/plugins/coreplugin/editormanager/editormanager_p.h b/src/plugins/coreplugin/editormanager/editormanager_p.h index 34a67457219..b130c497e4b 100644 --- a/src/plugins/coreplugin/editormanager/editormanager_p.h +++ b/src/plugins/coreplugin/editormanager/editormanager_p.h @@ -250,7 +250,8 @@ private: QAction *m_filePropertiesAction = nullptr; QAction *m_pinAction = nullptr; DocumentModel::Entry *m_contextMenuEntry = nullptr; - IEditor *m_contextMenuEditor = nullptr; + QPointer m_contextMenuDocument; + QPointer m_contextMenuEditor; OpenEditorsWindow *m_windowPopup = nullptr;