From ae56c60b12daaa5024795686ffc93310f698d7b8 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 8 Oct 2010 14:08:27 +0100
Subject: Fixing assorted memory leaks.

Resolves: #329

diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp
index d3bc6ce..c8f2f78 100644
--- a/Swift/Controllers/Chat/ChatsManager.cpp
+++ b/Swift/Controllers/Chat/ChatsManager.cpp
@@ -183,6 +183,7 @@ ChatController* ChatsManager::getChatControllerOrFindAnother(const JID &contact)
 }
 
 ChatController* ChatsManager::createNewChatController(const JID& contact) {
+	assert(chatControllers_.find(contact) == chatControllers_.end());
 	ChatController* controller = new ChatController(jid_, stanzaChannel_, iqRouter_, chatWindowFactory_, contact, nickResolver_, presenceOracle_, avatarManager_, mucRegistry_->isMUC(contact.toBare()), useDelayForLatency_, uiEventStream_, eventController_, timerFactory_, entityCapsManager_);
 	chatControllers_[contact] = controller;
 	controller->setAvailableServerFeatures(serverDiscoInfo_);
diff --git a/Swift/Controllers/EventWindowController.cpp b/Swift/Controllers/EventWindowController.cpp
index c02a352..fcafec9 100644
--- a/Swift/Controllers/EventWindowController.cpp
+++ b/Swift/Controllers/EventWindowController.cpp
@@ -29,6 +29,7 @@ void EventWindowController::handleEventQueueEventAdded(boost::shared_ptr<StanzaE
 void EventWindowController::handleEventConcluded(boost::shared_ptr<StanzaEvent> event) {
 	window_->removeEvent(event);
 	window_->addEvent(event, false);
+	event->onConclusion.disconnect(boost::bind(&EventWindowController::handleEventConcluded, this, event));
 }
 
 }
diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp
index 48a3aa9..55c99c5 100644
--- a/Swift/Controllers/RosterController.cpp
+++ b/Swift/Controllers/RosterController.cpp
@@ -67,6 +67,8 @@ RosterController::RosterController(const JID& jid, XMPPRoster* xmppRoster, Avata
 RosterController::~RosterController() {
 	delete offlineFilter_;
 	delete expandiness_;
+	delete mainWindow_;
+	delete roster_;
 }
 
 void RosterController::setNickResolver(NickResolver* nickResolver) {
diff --git a/Swift/Controllers/UnitTest/NickResolverTest.cpp b/Swift/Controllers/UnitTest/NickResolverTest.cpp
index f42a28a..1608731 100644
--- a/Swift/Controllers/UnitTest/NickResolverTest.cpp
+++ b/Swift/Controllers/UnitTest/NickResolverTest.cpp
@@ -51,6 +51,7 @@ class NickResolverTest : public CppUnit::TestFixture {
 			delete stanzaChannel_;
 			delete iqRouter_;
 			delete vCardStorage_;
+			delete xmppRoster_;
 		}
 
 		void testMUCNick() {
diff --git a/Swift/Controllers/UnitTest/RosterControllerTest.cpp b/Swift/Controllers/UnitTest/RosterControllerTest.cpp
index dde48ed..f49ed64 100644
--- a/Swift/Controllers/UnitTest/RosterControllerTest.cpp
+++ b/Swift/Controllers/UnitTest/RosterControllerTest.cpp
@@ -81,6 +81,7 @@ class RosterControllerTest : public CppUnit::TestFixture
 			delete stanzaChannel_;
 			delete uiEventStream_;
 			delete settings_;
+			delete xmppRoster_;
 		};
 
 	GroupRosterItem* groupChild(size_t i) {
diff --git a/Swift/Controllers/XMPPEvents/EventController.cpp b/Swift/Controllers/XMPPEvents/EventController.cpp
index 4e70978..381a5a1 100644
--- a/Swift/Controllers/XMPPEvents/EventController.cpp
+++ b/Swift/Controllers/XMPPEvents/EventController.cpp
@@ -31,6 +31,7 @@ void EventController::handleIncomingEvent(boost::shared_ptr<StanzaEvent> sourceE
 }
 
 void EventController::handleEventConcluded(boost::shared_ptr<StanzaEvent> event) {
+	event->onConclusion.disconnect(boost::bind(&EventController::handleEventConcluded, this, event));
 	events_.erase(std::remove(events_.begin(), events_.end(), event), events_.end());
 	onEventQueueLengthChange(events_.size());
 }
diff --git a/Swift/Controllers/XMPPEvents/EventController.h b/Swift/Controllers/XMPPEvents/EventController.h
index ba571df..61c5c47 100644
--- a/Swift/Controllers/XMPPEvents/EventController.h
+++ b/Swift/Controllers/XMPPEvents/EventController.h
@@ -17,7 +17,7 @@
 
 namespace Swift {
 	typedef std::vector<boost::shared_ptr<StanzaEvent> > EventList;
-	class EventController {
+	class EventController : public boost::bsignals::trackable{
 		public:
 
 			EventController();
diff --git a/Swift/Controllers/XMPPEvents/StanzaEvent.h b/Swift/Controllers/XMPPEvents/StanzaEvent.h
index 78dd4dc..321d23d 100644
--- a/Swift/Controllers/XMPPEvents/StanzaEvent.h
+++ b/Swift/Controllers/XMPPEvents/StanzaEvent.h
@@ -17,7 +17,8 @@ namespace Swift {
 			StanzaEvent() : time_(boost::posix_time::microsec_clock::universal_time()) {concluded_ = false;};
 			virtual ~StanzaEvent() {};
 			void conclude() {concluded_ = true; onConclusion();};
-			/** Do not call this directly from outside the class */
+			/** Do not call this directly from outside the class.
+			 * If you connect to this signal, you *must* disconnect from it manually. */
 			boost::signal<void()> onConclusion;
 			bool getConcluded() {return concluded_;};
 			boost::posix_time::ptime getTime() {return time_;}
diff --git a/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp b/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp
index 7e76d3a..33fc04a 100644
--- a/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp
+++ b/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp
@@ -37,6 +37,7 @@ class XMPPRosterControllerTest : public CppUnit::TestFixture
 		void tearDown() {
 			delete channel_;
 			delete router_;
+			delete xmppRoster_;
 		}
 
 		void testAdd() {
-- 
cgit v0.10.2-6-g49f6