From 56bdad140cf914d5e016972f773e3d31992af2a2 Mon Sep 17 00:00:00 2001
From: Eike Ziller <eike.ziller@digia.com>
Date: Fri, 19 Jul 2013 12:43:57 +0200
Subject: [PATCH] Fix crash with splitting and non-splittable editors

We may not just add an editor to a view without removing it from the
original view.

Task-number: QTCREATORBUG-9843
Change-Id: I1041ceb1f063bb627b5f0bd16bc3b2973a4dc3b6
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
---
 .../coreplugin/editormanager/editormanager.cpp      | 13 ++++++++++---
 .../coreplugin/editormanager/editormanager.h        |  2 +-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index c3d04a6a2ce..e898895f39a 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -1225,12 +1225,15 @@ void EditorManager::closeDuplicate(Core::IEditor *editor)
     }
 }
 
-Core::IEditor *EditorManager::pickUnusedEditor() const
+Core::IEditor *EditorManager::pickUnusedEditor(EditorView **foundView) const
 {
     foreach (IEditor *editor, openedEditors()) {
         EditorView *view = viewForEditor(editor);
-        if (!view || view->currentEditor() != editor)
+        if (!view || view->currentEditor() != editor) {
+            if (foundView)
+                *foundView = view;
             return editor;
+        }
     }
     return 0;
 }
@@ -1282,8 +1285,12 @@ Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core
                 view->addEditor(editor);
                 view->setCurrentEditor(editor);
                 if (!sourceView->currentEditor()) {
-                    if (IEditor *replacement = pickUnusedEditor())
+                    EditorView *replacementView = 0;
+                    if (IEditor *replacement = pickUnusedEditor(&replacementView)) {
+                        if (replacementView)
+                            replacementView->removeEditor(replacement);
                         sourceView->addEditor(replacement);
+                    }
                 }
                 return editor;
             } else if (duplicateSupported) {
diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h
index 556dfc65aa8..f76fc151936 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.h
+++ b/src/plugins/coreplugin/editormanager/editormanager.h
@@ -277,7 +277,7 @@ private:
     void closeView(Internal::EditorView *view);
     void emptyView(Internal::EditorView *view);
     static void splitNewWindow(Internal::EditorView *view);
-    IEditor *pickUnusedEditor() const;
+    IEditor *pickUnusedEditor(Internal::EditorView **foundView = 0) const;
     void addDocumentToRecentFiles(IDocument *document);
     void updateAutoSave();
     void setCloseSplitEnabled(Internal::SplitterOrView *splitterOrView, bool enable);
-- 
GitLab