From 1dfd78a9c8961604ec9550b605f63c272624c822 Mon Sep 17 00:00:00 2001
From: Christian Kamm <christian.d.kamm@nokia.com>
Date: Tue, 12 Jul 2011 08:52:32 +0200
Subject: [PATCH] Indenters: Fix dangling pointer when project settings are
 deleted.

Task-number: QTCREATORBUG-5390
Change-Id: I68517955a86fbb2ded53f6235a7fe27793e2b2c8
Reviewed-on: http://codereview.qt.nokia.com/1481
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
---
 src/plugins/cpptools/cppqtstyleindenter.cpp |  2 ++
 src/plugins/texteditor/basetexteditor.cpp   | 29 ++++++++++++++++++++-
 src/plugins/texteditor/basetexteditor.h     |  2 ++
 src/plugins/texteditor/indenter.cpp         |  1 +
 4 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/plugins/cpptools/cppqtstyleindenter.cpp b/src/plugins/cpptools/cppqtstyleindenter.cpp
index 413e404149d..3bcbe813c2c 100644
--- a/src/plugins/cpptools/cppqtstyleindenter.cpp
+++ b/src/plugins/cpptools/cppqtstyleindenter.cpp
@@ -50,6 +50,8 @@ using namespace CppTools;
 CppQtStyleIndenter::CppQtStyleIndenter()
     : m_cppCodeStylePreferences(0)
 {
+    // Just for safety. setCodeStylePreferences should be called when the editor the
+    // indenter belongs to gets initialized.
     m_cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStylePreferences();
 }
 
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 1401ab74500..a14afe32540 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -51,6 +51,7 @@
 #include "codeassistant.h"
 #include "defaultassistinterface.h"
 #include "convenience.h"
+#include "texteditorsettings.h"
 
 #include <aggregation/aggregate.h>
 #include <coreplugin/actionmanager/actionmanager.h>
@@ -4335,12 +4336,16 @@ void BaseTextEditorWidget::setTabPreferences(TabPreferences *tabPreferences)
 {
     if (d->m_tabPreferences) {
         disconnect(d->m_tabPreferences, SIGNAL(currentSettingsChanged(TextEditor::TabSettings)),
-                this, SLOT(setTabSettings(TextEditor::TabSettings)));
+                   this, SLOT(setTabSettings(TextEditor::TabSettings)));
+        disconnect(d->m_tabPreferences, SIGNAL(destroyed()),
+                   this, SLOT(onTabPreferencesDestroyed()));
     }
     d->m_tabPreferences = tabPreferences;
     if (d->m_tabPreferences) {
         connect(d->m_tabPreferences, SIGNAL(currentSettingsChanged(TextEditor::TabSettings)),
                 this, SLOT(setTabSettings(TextEditor::TabSettings)));
+        connect(d->m_tabPreferences, SIGNAL(destroyed()),
+                this, SLOT(onTabPreferencesDestroyed()));
         setTabSettings(d->m_tabPreferences->currentSettings());
     }
 }
@@ -4351,15 +4356,37 @@ void BaseTextEditorWidget::setCodeStylePreferences(IFallbackPreferences *prefere
     if (d->m_codeStylePreferences) {
         disconnect(d->m_codeStylePreferences, SIGNAL(currentValueChanged(QVariant)),
                 this, SLOT(slotCodeStyleSettingsChanged(QVariant)));
+        disconnect(d->m_codeStylePreferences, SIGNAL(destroyed()),
+                   this, SLOT(onCodeStylePreferencesDestroyed()));
     }
     d->m_codeStylePreferences = preferences;
     if (d->m_codeStylePreferences) {
         connect(d->m_codeStylePreferences, SIGNAL(currentValueChanged(QVariant)),
                 this, SLOT(slotCodeStyleSettingsChanged(QVariant)));
+        connect(d->m_codeStylePreferences, SIGNAL(destroyed()),
+                this, SLOT(onCodeStylePreferencesDestroyed()));
         slotCodeStyleSettingsChanged(d->m_codeStylePreferences->currentValue());
     }
 }
 
+void BaseTextEditorWidget::onTabPreferencesDestroyed()
+{
+    if (sender() != d->m_tabPreferences)
+        return;
+    // avoid failing disconnects, m_tabPreferences has already been reduced to QObject
+    d->m_tabPreferences = 0;
+    setTabPreferences(TextEditorSettings::instance()->tabPreferences(languageSettingsId()));
+}
+
+void BaseTextEditorWidget::onCodeStylePreferencesDestroyed()
+{
+    if (sender() != d->m_codeStylePreferences)
+        return;
+    // avoid failing disconnects, m_codeStylePreferences has already been reduced to QObject
+    d->m_codeStylePreferences = 0;
+    setCodeStylePreferences(TextEditorSettings::instance()->codeStylePreferences(languageSettingsId()));
+}
+
 void BaseTextEditorWidget::slotCodeStyleSettingsChanged(const QVariant &)
 {
 
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index e7915545d63..ab64861b305 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -355,6 +355,8 @@ private slots:
     bool inFindScope(const QTextCursor &cursor);
     bool inFindScope(int selectionStart, int selectionEnd);
     void inSnippetMode(bool *active);
+    void onTabPreferencesDestroyed();
+    void onCodeStylePreferencesDestroyed();
 
 private:
     Internal::BaseTextEditorPrivate *d;
diff --git a/src/plugins/texteditor/indenter.cpp b/src/plugins/texteditor/indenter.cpp
index 02e24e390eb..0695e4f3b79 100644
--- a/src/plugins/texteditor/indenter.cpp
+++ b/src/plugins/texteditor/indenter.cpp
@@ -33,6 +33,7 @@
 #include "indenter.h"
 #include "basetexteditor.h"
 #include "tabsettings.h"
+#include "ifallbackpreferences.h"
 
 using namespace TextEditor;
 
-- 
GitLab