summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2015-07-21 16:30:15 (GMT)
committerTobias Markmann <tm@ayena.de>2015-07-21 16:42:34 (GMT)
commit6ca201d0b48f4273e24dd7bff17c4a46eeaddf39 (patch)
treeace238716521c89a54168472905379a9249c3666
parent02ecf91d261276ec6f1e46b537ac0e10ebae3170 (diff)
downloadswift-6ca201d0b48f4273e24dd7bff17c4a46eeaddf39.zip
swift-6ca201d0b48f4273e24dd7bff17c4a46eeaddf39.tar.bz2
Explicitly disconnect from remaining resources in destructors
Explicitly disconnect from remaining resources in the ChainedConnected and ClientSessionStanzaChannel destructors, so the event loop will not call the signal handler methods on a freed object. Test-Information: Repeating the test case of creating a Swift::Client instance, connecting it and then deleting it after a random time below one second. On Mac OS X 10.9.5 running this test case causes two ASAN heap-use-after-free errors and with this patch the errors are gone. Change-Id: I3e48150c3633f4076ca9172aad9e85ba389df950
-rw-r--r--Swiften/Client/ClientSessionStanzaChannel.cpp15
-rw-r--r--Swiften/Client/ClientSessionStanzaChannel.h4
-rw-r--r--Swiften/Network/ChainedConnector.cpp15
-rw-r--r--Swiften/Network/ChainedConnector.h10
4 files changed, 34 insertions, 10 deletions
diff --git a/Swiften/Client/ClientSessionStanzaChannel.cpp b/Swiften/Client/ClientSessionStanzaChannel.cpp
index 3d7b9f7..3dc8c59 100644
--- a/Swiften/Client/ClientSessionStanzaChannel.cpp
+++ b/Swiften/Client/ClientSessionStanzaChannel.cpp
@@ -1,18 +1,29 @@
1/* 1/*
2 * Copyright (c) 2010 Isode Limited. 2 * Copyright (c) 2010-2015 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
6 6
7#include <Swiften/Client/ClientSessionStanzaChannel.h> 7#include <Swiften/Client/ClientSessionStanzaChannel.h>
8 8
9#include <boost/bind.hpp>
10#include <iostream> 9#include <iostream>
11 10
11#include <boost/bind.hpp>
12
12namespace Swift { 13namespace Swift {
13 14
15ClientSessionStanzaChannel::~ClientSessionStanzaChannel() {
16 if (session) {
17 session->onFinished.disconnect(boost::bind(&ClientSessionStanzaChannel::handleSessionFinished, this, _1));
18 session->onStanzaReceived.disconnect(boost::bind(&ClientSessionStanzaChannel::handleStanza, this, _1));
19 session->onStanzaAcked.disconnect(boost::bind(&ClientSessionStanzaChannel::handleStanzaAcked, this, _1));
20 session->onInitialized.disconnect(boost::bind(&ClientSessionStanzaChannel::handleSessionInitialized, this));
21 session.reset();
22 }
23}
24
14void ClientSessionStanzaChannel::setSession(boost::shared_ptr<ClientSession> session) { 25void ClientSessionStanzaChannel::setSession(boost::shared_ptr<ClientSession> session) {
15 assert(!this->session); 26 assert(!this->session);
16 this->session = session; 27 this->session = session;
17 session->onInitialized.connect(boost::bind(&ClientSessionStanzaChannel::handleSessionInitialized, this)); 28 session->onInitialized.connect(boost::bind(&ClientSessionStanzaChannel::handleSessionInitialized, this));
18 session->onFinished.connect(boost::bind(&ClientSessionStanzaChannel::handleSessionFinished, this, _1)); 29 session->onFinished.connect(boost::bind(&ClientSessionStanzaChannel::handleSessionFinished, this, _1));
diff --git a/Swiften/Client/ClientSessionStanzaChannel.h b/Swiften/Client/ClientSessionStanzaChannel.h
index 1104416..0ffcd9d 100644
--- a/Swiften/Client/ClientSessionStanzaChannel.h
+++ b/Swiften/Client/ClientSessionStanzaChannel.h
@@ -10,20 +10,22 @@
10 10
11#include <Swiften/Base/API.h> 11#include <Swiften/Base/API.h>
12#include <Swiften/Base/IDGenerator.h> 12#include <Swiften/Base/IDGenerator.h>
13#include <Swiften/Client/ClientSession.h> 13#include <Swiften/Client/ClientSession.h>
14#include <Swiften/Client/StanzaChannel.h> 14#include <Swiften/Client/StanzaChannel.h>
15#include <Swiften/Elements/Message.h>
16#include <Swiften/Elements/IQ.h> 15#include <Swiften/Elements/IQ.h>
16#include <Swiften/Elements/Message.h>
17#include <Swiften/Elements/Presence.h> 17#include <Swiften/Elements/Presence.h>
18 18
19namespace Swift { 19namespace Swift {
20 /** 20 /**
21 * StanzaChannel implementation around a ClientSession. 21 * StanzaChannel implementation around a ClientSession.
22 */ 22 */
23 class SWIFTEN_API ClientSessionStanzaChannel : public StanzaChannel { 23 class SWIFTEN_API ClientSessionStanzaChannel : public StanzaChannel {
24 public: 24 public:
25 virtual ~ClientSessionStanzaChannel();
26
25 void setSession(boost::shared_ptr<ClientSession> session); 27 void setSession(boost::shared_ptr<ClientSession> session);
26 28
27 void sendIQ(boost::shared_ptr<IQ> iq); 29 void sendIQ(boost::shared_ptr<IQ> iq);
28 void sendMessage(boost::shared_ptr<Message> message); 30 void sendMessage(boost::shared_ptr<Message> message);
29 void sendPresence(boost::shared_ptr<Presence> presence); 31 void sendPresence(boost::shared_ptr<Presence> presence);
diff --git a/Swiften/Network/ChainedConnector.cpp b/Swiften/Network/ChainedConnector.cpp
index ac48d20..3cc4057 100644
--- a/Swiften/Network/ChainedConnector.cpp
+++ b/Swiften/Network/ChainedConnector.cpp
@@ -1,20 +1,21 @@
1/* 1/*
2 * Copyright (c) 2011 Isode Limited. 2 * Copyright (c) 2011-2015 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
6 6
7#include <Swiften/Network/ChainedConnector.h> 7#include <Swiften/Network/ChainedConnector.h>
8 8
9#include <boost/bind.hpp>
10#include <typeinfo> 9#include <typeinfo>
11 10
11#include <boost/bind.hpp>
12
12#include <Swiften/Base/Log.h> 13#include <Swiften/Base/Log.h>
13#include <Swiften/Base/foreach.h> 14#include <Swiften/Base/foreach.h>
14#include <Swiften/Network/Connector.h>
15#include <Swiften/Network/ConnectionFactory.h> 15#include <Swiften/Network/ConnectionFactory.h>
16#include <Swiften/Network/Connector.h>
16 17
17using namespace Swift; 18using namespace Swift;
18 19
19ChainedConnector::ChainedConnector( 20ChainedConnector::ChainedConnector(
20 const std::string& hostname, 21 const std::string& hostname,
@@ -30,10 +31,18 @@ ChainedConnector::ChainedConnector(
30 connectionFactories(connectionFactories), 31 connectionFactories(connectionFactories),
31 timerFactory(timerFactory), 32 timerFactory(timerFactory),
32 timeoutMilliseconds(0) { 33 timeoutMilliseconds(0) {
33} 34}
34 35
36ChainedConnector::~ChainedConnector() {
37 if (currentConnector) {
38 currentConnector->onConnectFinished.disconnect(boost::bind(&ChainedConnector::handleConnectorFinished, this, _1, _2));
39 currentConnector->stop();
40 currentConnector.reset();
41 }
42}
43
35void ChainedConnector::setTimeoutMilliseconds(int milliseconds) { 44void ChainedConnector::setTimeoutMilliseconds(int milliseconds) {
36 timeoutMilliseconds = milliseconds; 45 timeoutMilliseconds = milliseconds;
37} 46}
38 47
39void ChainedConnector::start() { 48void ChainedConnector::start() {
diff --git a/Swiften/Network/ChainedConnector.h b/Swiften/Network/ChainedConnector.h
index 77fa6fd..9bcc961 100644
--- a/Swiften/Network/ChainedConnector.h
+++ b/Swiften/Network/ChainedConnector.h
@@ -1,22 +1,23 @@
1/* 1/*
2 * Copyright (c) 2011 Isode Limited. 2 * Copyright (c) 2011-2015 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
6 6
7#pragma once 7#pragma once
8 8
9#include <deque>
9#include <string> 10#include <string>
10#include <vector> 11#include <vector>
11#include <deque> 12
12#include <boost/shared_ptr.hpp>
13#include <boost/optional.hpp> 13#include <boost/optional.hpp>
14#include <boost/shared_ptr.hpp>
14 15
15#include <Swiften/Base/API.h> 16#include <Swiften/Base/API.h>
16#include <Swiften/Base/boost_bsignals.h>
17#include <Swiften/Base/Error.h> 17#include <Swiften/Base/Error.h>
18#include <Swiften/Base/boost_bsignals.h>
18 19
19namespace Swift { 20namespace Swift {
20 class Connection; 21 class Connection;
21 class Connector; 22 class Connector;
22 class ConnectionFactory; 23 class ConnectionFactory;
@@ -24,10 +25,11 @@ namespace Swift {
24 class DomainNameResolver; 25 class DomainNameResolver;
25 26
26 class SWIFTEN_API ChainedConnector { 27 class SWIFTEN_API ChainedConnector {
27 public: 28 public:
28 ChainedConnector(const std::string& hostname, int port, const boost::optional<std::string>& serviceLookupPrefix, DomainNameResolver*, const std::vector<ConnectionFactory*>&, TimerFactory*); 29 ChainedConnector(const std::string& hostname, int port, const boost::optional<std::string>& serviceLookupPrefix, DomainNameResolver*, const std::vector<ConnectionFactory*>&, TimerFactory*);
30 ~ChainedConnector();
29 31
30 void setTimeoutMilliseconds(int milliseconds); 32 void setTimeoutMilliseconds(int milliseconds);
31 void start(); 33 void start();
32 void stop(); 34 void stop();
33 35