From f10bd4cc1f570b27943d27e536d1dcfbcd55ec6a Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Fri, 17 Jul 2015 10:18:04 +0200
Subject: Do not reallocate FileTransferManager on connect of Client

Now Client allocates a FileTransferManager in its ctor and frees
it in its dtor, like all the other resources of the Client class.

This fixes an ASAN crash in the use case where you do a file
transfer, go offline and online again and try to start another
file transfer.

Test-Information:

Tested that the mentioned use case does not crash anymore,
that file transfers still work, and that FileTransferTest still
passes.

Change-Id: Iddbcd8522af7df528bdc2b030fe616ad3f0c4701

diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp
index 613249b..3bfdd3f 100644
--- a/Swiften/Client/Client.cpp
+++ b/Swiften/Client/Client.cpp
@@ -71,7 +71,6 @@ Client::Client(const JID& jid, const SafeString& password, NetworkFactories* net
 	
 	jingleSessionManager = new JingleSessionManager(getIQRouter());
 	blockListManager = new ClientBlockListManager(getIQRouter());
-	fileTransferManager = NULL;
 
 	whiteboardSessionManager = NULL;
 #ifdef SWIFT_EXPERIMENTAL_WB
@@ -79,6 +78,24 @@ Client::Client(const JID& jid, const SafeString& password, NetworkFactories* net
 #endif
 
 	pubsubManager = new PubSubManagerImpl(getStanzaChannel(), getIQRouter());
+
+#ifdef SWIFT_EXPERIMENTAL_FT
+	fileTransferManager = new FileTransferManagerImpl(
+			getJID(),
+			jingleSessionManager,
+			getIQRouter(),
+			getEntityCapsProvider(),
+			presenceOracle,
+			getNetworkFactories()->getConnectionFactory(),
+			getNetworkFactories()->getConnectionServerFactory(),
+			getNetworkFactories()->getTimerFactory(),
+			getNetworkFactories()->getDomainNameResolver(),
+			getNetworkFactories()->getNetworkEnvironment(),
+			getNetworkFactories()->getNATTraverser(),
+			getNetworkFactories()->getCryptoProvider());
+#else
+	fileTransferManager = new DummyFileTransferManager();
+#endif
 }
 
 Client::~Client() {
@@ -126,24 +143,6 @@ void Client::setSoftwareVersion(const std::string& name, const std::string& vers
 }
 
 void Client::handleConnected() {
-	delete fileTransferManager;
-#ifdef SWIFT_EXPERIMENTAL_FT
-	fileTransferManager = new FileTransferManagerImpl(
-			getJID(), 
-			jingleSessionManager, 
-			getIQRouter(), 
-			getEntityCapsProvider(), 
-			presenceOracle, 
-			getNetworkFactories()->getConnectionFactory(), 
-			getNetworkFactories()->getConnectionServerFactory(), 
-			getNetworkFactories()->getTimerFactory(), 
-			getNetworkFactories()->getDomainNameResolver(),
-			getNetworkFactories()->getNetworkEnvironment(),
-			getNetworkFactories()->getNATTraverser(),
-			getNetworkFactories()->getCryptoProvider());
-#else
-	fileTransferManager = new DummyFileTransferManager();
-#endif
 	discoManager->handleConnected();
 }
 
diff --git a/Swiften/FileTransfer/FileTransferManagerImpl.cpp b/Swiften/FileTransfer/FileTransferManagerImpl.cpp
index ab08c45..f4f9517 100644
--- a/Swiften/FileTransfer/FileTransferManagerImpl.cpp
+++ b/Swiften/FileTransfer/FileTransferManagerImpl.cpp
@@ -13,34 +13,34 @@
 #include <Swiften/FileTransfer/FileTransferManagerImpl.h>
 
 #include <boost/bind.hpp>
-#include <boost/filesystem.hpp>
 #include <boost/cstdint.hpp>
+#include <boost/filesystem.hpp>
 
-#include <Swiften/Base/foreach.h>
+#include "Swiften/Disco/EntityCapsProvider.h"
+#include <Swiften/Base/BoostFilesystemVersion.h>
 #include <Swiften/Base/Log.h>
 #include <Swiften/Base/Path.h>
-#include "Swiften/Disco/EntityCapsProvider.h"
-#include <Swiften/JID/JID.h>
+#include <Swiften/Base/foreach.h>
 #include <Swiften/Elements/JingleFileTransferFileInfo.h>
-#include <Swiften/FileTransfer/SOCKS5BytestreamServerManager.h>
-#include <Swiften/FileTransfer/OutgoingFileTransferManager.h>
-#include <Swiften/FileTransfer/IncomingFileTransferManager.h>
+#include <Swiften/Elements/Presence.h>
 #include <Swiften/FileTransfer/DefaultFileTransferTransporterFactory.h>
-#include <Swiften/FileTransfer/SOCKS5BytestreamRegistry.h>
+#include <Swiften/FileTransfer/IncomingFileTransferManager.h>
+#include <Swiften/FileTransfer/OutgoingFileTransferManager.h>
 #include <Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.h>
-#include <Swiften/Presence/PresenceOracle.h>
-#include <Swiften/Elements/Presence.h>
+#include <Swiften/FileTransfer/SOCKS5BytestreamRegistry.h>
+#include <Swiften/FileTransfer/SOCKS5BytestreamServerManager.h>
+#include <Swiften/JID/JID.h>
 #include <Swiften/Network/ConnectionFactory.h>
 #include <Swiften/Network/ConnectionServerFactory.h>
 #include <Swiften/Network/HostAddress.h>
 #include <Swiften/Network/NATTraverser.h>
-
-#include <Swiften/Base/BoostFilesystemVersion.h>
+#include <Swiften/Presence/PresenceOracle.h>
+#include <Swiften/Queries/IQRouter.h>
 
 namespace Swift {
 
 FileTransferManagerImpl::FileTransferManagerImpl(
-		const JID& ownFullJID, 
+		const JID& ownJID,
 		JingleSessionManager* jingleSessionManager, 
 		IQRouter* router, 
 		EntityCapsProvider* capsProvider, 
@@ -52,16 +52,13 @@ FileTransferManagerImpl::FileTransferManagerImpl(
 		NetworkEnvironment* networkEnvironment,
 		NATTraverser* natTraverser,
 		CryptoProvider* crypto) : 
-			ownJID(ownFullJID), 
 			iqRouter(router), 
 			capsProvider(capsProvider), 
 			presenceOracle(presOracle) {
-	assert(!ownFullJID.isBare());
-
 	bytestreamRegistry = new SOCKS5BytestreamRegistry();
 	s5bServerManager = new SOCKS5BytestreamServerManager(
 			bytestreamRegistry, connectionServerFactory, networkEnvironment, natTraverser);
-	bytestreamProxy = new SOCKS5BytestreamProxiesManager(connectionFactory, timerFactory, domainNameResolver, iqRouter, JID(ownFullJID.getDomain()));
+	bytestreamProxy = new SOCKS5BytestreamProxiesManager(connectionFactory, timerFactory, domainNameResolver, iqRouter, JID(ownJID.getDomain()));
 
 	transporterFactory = new DefaultFileTransferTransporterFactory(
 			bytestreamRegistry,
@@ -166,8 +163,10 @@ OutgoingFileTransfer::ref FileTransferManagerImpl::createOutgoingFileTransfer(
 			return OutgoingFileTransfer::ref();
 		}
 	}
-	
-	return outgoingFTManager->createOutgoingFileTransfer(ownJID, receipient, bytestream, fileInfo, config);
+
+	assert(!iqRouter->getJID().isBare());
+
+	return outgoingFTManager->createOutgoingFileTransfer(iqRouter->getJID(), receipient, bytestream, fileInfo, config);
 }
 
 }
diff --git a/Swiften/FileTransfer/FileTransferManagerImpl.h b/Swiften/FileTransfer/FileTransferManagerImpl.h
index de6e857..7a77564 100644
--- a/Swiften/FileTransfer/FileTransferManagerImpl.h
+++ b/Swiften/FileTransfer/FileTransferManagerImpl.h
@@ -12,23 +12,22 @@
 
 #pragma once
 
-#include <vector>
 #include <string>
+#include <vector>
 
-#include <boost/filesystem/path.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/filesystem/path.hpp>
 #include <boost/optional.hpp>
 
 #include <Swiften/Base/API.h>
+#include <Swiften/Base/IDGenerator.h>
 #include <Swiften/Base/Override.h>
+#include <Swiften/Base/boost_bsignals.h>
+#include <Swiften/Elements/S5BProxyRequest.h>
 #include <Swiften/FileTransfer/FileTransferManager.h>
 #include <Swiften/FileTransfer/FileTransferOptions.h>
-#include <Swiften/Base/boost_bsignals.h>
-#include <Swiften/Base/IDGenerator.h>
-#include <Swiften/JID/JID.h>
-#include <Swiften/FileTransfer/OutgoingFileTransfer.h>
 #include <Swiften/FileTransfer/IncomingFileTransfer.h>
-#include <Swiften/Elements/S5BProxyRequest.h>
+#include <Swiften/FileTransfer/OutgoingFileTransfer.h>
 
 namespace Swift {
 	class ConnectionFactory;
@@ -39,6 +38,7 @@ namespace Swift {
 	class FileTransferTransporterFactory;
 	class IQRouter;
 	class IncomingFileTransferManager;
+	class JID;
 	class JingleSessionManager;
 	class NATTraverser;
 	class NetworkEnvironment;
@@ -89,8 +89,6 @@ namespace Swift {
 			boost::optional<JID> highestPriorityJIDSupportingFileTransfer(const JID& bareJID);
 			
 		private:
-			JID ownJID;
-			
 			OutgoingFileTransferManager* outgoingFTManager;
 			IncomingFileTransferManager* incomingFTManager;
 			FileTransferTransporterFactory* transporterFactory;
-- 
cgit v0.10.2-6-g49f6