Skip to content

Commit e7f76bc

Browse files
the TcpConnection class no longer calls back to userspace / to the user-supplied handler if user-space explicitly destructs the object
1 parent a75b3d5 commit e7f76bc

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

include/amqpcpp/linux_tcp/tcpconnection.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class TcpConnection :
9191
virtual void onHeartbeat(Connection *connection) override
9292
{
9393
// pass on to tcp handler
94-
_handler->onHeartbeat(this);
94+
if (_handler) _handler->onHeartbeat(this);
9595
}
9696

9797
/**
@@ -108,7 +108,7 @@ class TcpConnection :
108108
virtual void onReady(Connection *connection) override
109109
{
110110
// pass on to the handler
111-
_handler->onReady(this);
111+
if (_handler) _handler->onReady(this);
112112
}
113113

114114
/**
@@ -124,7 +124,7 @@ class TcpConnection :
124124
virtual void onConnected(TcpState *state) override
125125
{
126126
// pass on to the handler
127-
_handler->onConnected(this);
127+
if (_handler) _handler->onConnected(this);
128128
}
129129

130130
/**
@@ -136,7 +136,7 @@ class TcpConnection :
136136
virtual bool onSecured(TcpState *state, const SSL *ssl) override
137137
{
138138
// pass on to user-space
139-
return _handler->onSecured(this, ssl);
139+
return _handler && _handler->onSecured(this, ssl);
140140
}
141141

142142
/**
@@ -160,7 +160,7 @@ class TcpConnection :
160160
virtual void onIdle(TcpState *state, int socket, int events) override
161161
{
162162
// pass on to user-space
163-
return _handler->monitor(this, socket, events);
163+
if (_handler) _handler->monitor(this, socket, events);
164164
}
165165

166166
/**

src/linux_tcp/tcpconnection.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Implementation file for the TCP connection
55
*
66
* @author Emiel Bruijntjes <[email protected]>
7-
* @copyright 2015 - 2018 Copernica BV
7+
* @copyright 2015 - 2020 Copernica BV
88
*/
99

1010
/**
@@ -36,7 +36,16 @@ TcpConnection::TcpConnection(TcpHandler *handler, const Address &address) :
3636
/**
3737
* Destructor
3838
*/
39-
TcpConnection::~TcpConnection() noexcept = default;
39+
TcpConnection::~TcpConnection() noexcept
40+
{
41+
// When the object is destructed, the _state-pointer will also destruct, resulting
42+
// in some final calls back to us to inform that the connection has indeed been closed.
43+
// This normally results in calls back to user-space (via the _handler pointer) but
44+
// since user-space is apparently no longer interested in the TcpConnection (why would
45+
// it otherwise prematurely destruct the object?) we reset the handler-pointer to
46+
// prevent that such calls back to userspace take place
47+
_handler = nullptr;
48+
}
4049

4150
/**
4251
* The filedescriptor that is used for this connection
@@ -121,7 +130,7 @@ bool TcpConnection::close(bool immediate)
121130
if (!monitor.valid()) return true;
122131

123132
// tell the handler that the connection was closed
124-
if (failed) _handler->onError(this, "connection prematurely closed by client");
133+
if (failed && _handler) _handler->onError(this, "connection prematurely closed by client");
125134

126135
// stop if object was destructed
127136
if (!monitor.valid()) return true;
@@ -143,7 +152,7 @@ bool TcpConnection::close(bool immediate)
143152
void TcpConnection::onProperties(Connection *connection, const Table &server, Table &client)
144153
{
145154
// tell the handler
146-
return _handler->onProperties(this, server, client);
155+
if (_handler) _handler->onProperties(this, server, client);
147156
}
148157

149158
/**
@@ -158,7 +167,7 @@ uint16_t TcpConnection::onNegotiate(Connection *connection, uint16_t interval)
158167
_state->maxframe(connection->maxFrame());
159168

160169
// tell the handler
161-
return _handler->onNegotiate(this, interval);
170+
return _handler ? _handler->onNegotiate(this, interval) : interval;
162171
}
163172

164173
/**
@@ -184,7 +193,7 @@ void TcpConnection::onError(Connection *connection, const char *message)
184193
Monitor monitor(this);
185194

186195
// tell this to the user
187-
_handler->onError(this, message);
196+
if (_handler) _handler->onError(this, message);
188197

189198
// object could be destructed by user-space
190199
if (!monitor.valid()) return;
@@ -214,6 +223,9 @@ void TcpConnection::onClosed(Connection *connection)
214223
*/
215224
void TcpConnection::onError(TcpState *state, const char *message, bool connected)
216225
{
226+
// if user-space is no longer interested in this object, the rest of the code is pointless here
227+
if (_handler == nullptr) return;
228+
217229
// monitor to check if all operations are active
218230
Monitor monitor(this);
219231

@@ -240,6 +252,9 @@ void TcpConnection::onError(TcpState *state, const char *message, bool connected
240252
*/
241253
void TcpConnection::onLost(TcpState *state)
242254
{
255+
// if user-space is no longer interested in this object, the rest of the code is pointless here
256+
if (_handler == nullptr) return;
257+
243258
// monitor to check if "this" is destructed
244259
Monitor monitor(this);
245260

0 commit comments

Comments
 (0)