Fix segfault when the handler is destructed from within a callback

This commit is contained in:
Martijn Otto 2016-01-15 14:19:09 +01:00
parent 25ce57818a
commit 89c2075a5f
4 changed files with 45 additions and 29 deletions

View File

@ -85,6 +85,14 @@ private:
ev_io_start(_loop, &_io); ev_io_start(_loop, &_io);
} }
/**
* Watchers cannot be copied or moved
*
* @param that The object to not move or copy
*/
Watcher(Watcher &&that) = delete;
Watcher(const Watcher &that) = delete;
/** /**
* Destructor * Destructor
*/ */

View File

@ -26,7 +26,9 @@ class TcpState;
/** /**
* Class definition * Class definition
*/ */
class TcpConnection : private ConnectionHandler class TcpConnection :
private ConnectionHandler,
private Watchable
{ {
private: private:
/** /**

View File

@ -47,9 +47,15 @@ TcpConnection::~TcpConnection()
*/ */
void TcpConnection::process(int fd, int flags) void TcpConnection::process(int fd, int flags)
{ {
// monitor the object for destruction
Monitor monitor{ this };
// pass on the the state, that returns a new impl // pass on the the state, that returns a new impl
auto *result = _state->process(fd, flags); auto *result = _state->process(fd, flags);
// are we still valid
if (!monitor.valid()) return;
// skip if the same state is continued to be used, or when the process() // skip if the same state is continued to be used, or when the process()
// method returns nullptr (which only happens when the object is destructed, // method returns nullptr (which only happens when the object is destructed,
// and "this" is no longer valid) // and "this" is no longer valid)