From da26b1112e09fdd09469710f692fd98423f6cd75 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 13 Mar 2020 09:44:29 +0100 Subject: [PATCH] prevent crash when channelimpl unregistered from the connectionimpl, (hopefully) fixes #299 and fixes #326 --- src/channelimpl.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/channelimpl.cpp b/src/channelimpl.cpp index 87508c9..6f128ea 100644 --- a/src/channelimpl.cpp +++ b/src/channelimpl.cpp @@ -855,10 +855,20 @@ void ChannelImpl::reportError(const char *message, bool notifyhandler) // leap out if object no longer exists if (!monitor.valid()) return; + + // done when the channel was already no longer associated with a connection + if (!_connection) return; + + // when we call _connection->remove(this), it is possible that the last reference + // to the channelimpl is also dropped and that the object immediately destructs, + // this is something that we want to prevent so we create an extra reference to the object here + auto self = shared_from_this(); // the connection no longer has to know that this channel exists, // because the channel ID is no longer in use - if (_connection) _connection->remove(this); + _connection->remove(this); + + // remember that we're no longer associated with a connection _connection = nullptr; }