From 1bdcf392e7965e5f789b3b3fd026931c61a1cc59 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Sun, 8 Mar 2015 17:27:27 +0100
Subject: Add an EventOwner to NATTraversalRequests so that accociated objects
 are not released early

This patch also disconnects possible slots on stop() from the signals.

Test-Infromation:

This fixes a crash that occured during testing with
ConcurrentFileTransferTest.

Change-Id: I2b443f40ac3359c8142fcffc084647312b9a1e76

diff --git a/Swiften/Network/PlatformNATTraversalWorker.cpp b/Swiften/Network/PlatformNATTraversalWorker.cpp
index 5e2bc0c..ef1876c 100644
--- a/Swiften/Network/PlatformNATTraversalWorker.cpp
+++ b/Swiften/Network/PlatformNATTraversalWorker.cpp
@@ -12,6 +12,7 @@
 
 #include <Swiften/Base/Log.h>
 #include <Swiften/EventLoop/EventLoop.h>
+#include <Swiften/EventLoop/EventOwner.h>
 #include <Swiften/Network/NATTraversalGetPublicIPRequest.h>
 #include <Swiften/Network/NATTraversalForwardPortRequest.h>
 #include <Swiften/Network/NATTraversalRemovePortForwardingRequest.h>
@@ -24,7 +25,7 @@
 
 namespace Swift {
 
-class PlatformNATTraversalRequest : public boost::enable_shared_from_this<PlatformNATTraversalRequest> {
+class PlatformNATTraversalRequest : public boost::enable_shared_from_this<PlatformNATTraversalRequest>, public EventOwner {
 	public:
 		typedef boost::shared_ptr<PlatformNATTraversalRequest> ref;
 
@@ -59,16 +60,19 @@ class PlatformNATTraversalGetPublicIPRequest : public NATTraversalGetPublicIPReq
 		PlatformNATTraversalGetPublicIPRequest(PlatformNATTraversalWorker* worker) : PlatformNATTraversalRequest(worker) {
 		}
 
+		virtual ~PlatformNATTraversalGetPublicIPRequest() {
+		}
+
 		virtual void start() {
 			doRun();
 		}
 
 		virtual void stop() {
-			// TODO
+			onResult.disconnect_all_slots();
 		}
 
 		virtual void runBlocking() {
-			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->getPublicIP()));
+			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->getPublicIP()), shared_from_this());
 		}
 };
 
@@ -77,16 +81,19 @@ class PlatformNATTraversalForwardPortRequest : public NATTraversalForwardPortReq
 		PlatformNATTraversalForwardPortRequest(PlatformNATTraversalWorker* worker, unsigned int localIP, unsigned int publicIP) : PlatformNATTraversalRequest(worker), localIP(localIP), publicIP(publicIP) {
 		}
 
+		virtual ~PlatformNATTraversalForwardPortRequest() {
+		}
+
 		virtual void start() {
 			doRun();
 		}
 
 		virtual void stop() {
-			// TODO
+			onResult.disconnect_all_slots();
 		}
 
 		virtual void runBlocking() {
-			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->addPortForward(boost::numeric_cast<int>(localIP), boost::numeric_cast<int>(publicIP))));
+			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->addPortForward(boost::numeric_cast<int>(localIP), boost::numeric_cast<int>(publicIP))), shared_from_this());
 		}
 
 	private:
@@ -99,16 +106,19 @@ class PlatformNATTraversalRemovePortForwardingRequest : public NATTraversalRemov
 		PlatformNATTraversalRemovePortForwardingRequest(PlatformNATTraversalWorker* worker, const NATPortMapping& mapping) : PlatformNATTraversalRequest(worker), mapping(mapping) {
 		}
 
+		virtual ~PlatformNATTraversalRemovePortForwardingRequest() {
+		}
+
 		virtual void start() {
 			doRun();
 		}
 
 		virtual void stop() {
-			// TODO
+			onResult.disconnect_all_slots();
 		}
 
 		virtual void runBlocking() {
-			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->removePortForward(mapping)));
+			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->removePortForward(mapping)), shared_from_this());
 		}
 
 	private:
-- 
cgit v0.10.2-6-g49f6