fixed issue that a tcp connection could crash if the user space code deconstructed a handler in a onClosed() handler

This commit is contained in:
Emiel Bruijntjes 2017-04-26 15:33:17 +02:00
parent af6886be92
commit 16e2d21aa3
2 changed files with 28 additions and 1 deletions

View File

@ -133,6 +133,9 @@ public:
*/ */
virtual ~TcpConnected() noexcept virtual ~TcpConnected() noexcept
{ {
// skip if handler is already forgotten
if (_handler == nullptr) return;
// we no longer have to monitor the socket // we no longer have to monitor the socket
_handler->monitor(_connection, _socket, 0); _handler->monitor(_connection, _socket, 0);
@ -263,6 +266,30 @@ public:
// pass to base // pass to base
return TcpState::reportNegotiate(heartbeat); return TcpState::reportNegotiate(heartbeat);
} }
/**
* Report to the handler that the connection was nicely closed
*/
virtual void reportClosed() override
{
// we no longer have to monitor the socket
_handler->monitor(_connection, _socket, 0);
// close the socket
close(_socket);
// socket is closed now
_socket = -1;
// copy the handler (if might destruct this object)
auto *handler = _handler;
// reset member before the handler can make a mess of it
_handler = nullptr;
// notify to handler
handler->onClosed(_connection);
}
}; };
/** /**

View File

@ -127,7 +127,7 @@ public:
/** /**
* Report to the handler that the connection was nicely closed * Report to the handler that the connection was nicely closed
*/ */
void reportClosed() virtual void reportClosed()
{ {
// pass to handler // pass to handler
_handler->onClosed(_connection); _handler->onClosed(_connection);