From 0b9db0c080dbfeef6d209544afcc120712ab445e Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 13 May 2011 20:37:33 +0100
Subject: Don't crash when chat windows are deleted.

Resolves: #879

diff --git a/Swift/QtUI/QtUIFactory.cpp b/Swift/QtUI/QtUIFactory.cpp
index ffbe3c0..40ce95e 100644
--- a/Swift/QtUI/QtUIFactory.cpp
+++ b/Swift/QtUI/QtUIFactory.cpp
@@ -86,12 +86,19 @@ MUCSearchWindow* QtUIFactory::createMUCSearchWindow() {
 ChatWindow* QtUIFactory::createChatWindow(const JID& contact, UIEventStream* eventStream) {
 	QtChatWindow* window = dynamic_cast<QtChatWindow*>(chatWindowFactory->createChatWindow(contact, eventStream));
 	chatWindows.push_back(window);
-	foreach (QtChatWindow* existingWindow, chatWindows) {
-		connect(window, SIGNAL(fontResized(int)), existingWindow, SLOT(handleFontResized(int)));
-		connect(existingWindow, SIGNAL(fontResized(int)), window, SLOT(handleFontResized(int)));
+	std::vector<QPointer<QtChatWindow> > deletions;
+	foreach (QPointer<QtChatWindow> existingWindow, chatWindows) {
+		if (existingWindow.isNull()) {
+			deletions.push_back(existingWindow);
+		} else {
+			connect(window, SIGNAL(fontResized(int)), existingWindow, SLOT(handleFontResized(int)));
+			connect(existingWindow, SIGNAL(fontResized(int)), window, SLOT(handleFontResized(int)));
+		}
+	}
+	foreach (QPointer<QtChatWindow> deletedWindow, deletions) {
+		chatWindows.erase(std::remove(chatWindows.begin(), chatWindows.end(), deletedWindow), chatWindows.end());
 	}
 	connect(window, SIGNAL(fontResized(int)), this, SLOT(handleChatWindowFontResized(int)));
-	connect(window, SIGNAL(destroyed(QObject*)), this, SLOT(handleChatWindowDestroyed(QObject*)));
 	window->handleFontResized(chatFontSize);
 	return window;
 }
@@ -101,11 +108,6 @@ void QtUIFactory::handleChatWindowFontResized(int size) {
 	settings->storeInt(CHATWINDOW_FONT_SIZE, size);
 }
 
-void QtUIFactory::handleChatWindowDestroyed(QObject* window) {
-	QtChatWindow* chatWindow = qobject_cast<QtChatWindow*>(window);
-	chatWindows.erase(std::remove(chatWindows.begin(), chatWindows.end(), chatWindow), chatWindows.end());
-}
-
 UserSearchWindow* QtUIFactory::createUserSearchWindow(UserSearchWindow::Type type, UIEventStream* eventStream, const std::set<std::string>& groups) {
 	return new QtUserSearchWindow(eventStream, type, groups);
 };
diff --git a/Swift/QtUI/QtUIFactory.h b/Swift/QtUI/QtUIFactory.h
index 828f1b4..a576ded 100644
--- a/Swift/QtUI/QtUIFactory.h
+++ b/Swift/QtUI/QtUIFactory.h
@@ -7,6 +7,7 @@
 #pragma once
 
 #include <QObject>
+#include <QPointer>
 
 #include <Swift/Controllers/UIInterfaces/UIFactory.h>
 
@@ -42,7 +43,6 @@ namespace Swift {
 
 		private slots:
 			void handleLoginWindowGeometryChanged();
-			void handleChatWindowDestroyed(QObject*);
 			void handleChatWindowFontResized(int);
 
 		private:
@@ -53,7 +53,7 @@ namespace Swift {
 			QtChatWindowFactory* chatWindowFactory;
 			QtMainWindow* lastMainWindow;
 			QtLoginWindow* loginWindow;
-			std::vector<QtChatWindow*> chatWindows;
+			std::vector<QPointer<QtChatWindow> > chatWindows;
 			bool startMinimized;
 			int chatFontSize;
 	};
-- 
cgit v0.10.2-6-g49f6