From d488f83317534f35e7e7fc8e09925108393680fe Mon Sep 17 00:00:00 2001 From: Louis <3095746912@qq.com> Date: Sat, 9 May 2026 17:11:07 +0800 Subject: [PATCH] Add channel_->tie() in Connector::connecting() to fix lifetime safety --- muduo/net/Connector.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/muduo/net/Connector.cc b/muduo/net/Connector.cc index 0b492ad95..3fb947f32 100644 --- a/muduo/net/Connector.cc +++ b/muduo/net/Connector.cc @@ -130,10 +130,11 @@ void Connector::connecting(int sockfd) setState(kConnecting); assert(!channel_); channel_.reset(new Channel(loop_, sockfd)); + channel_->tie(shared_from_this()); channel_->setWriteCallback( - std::bind(&Connector::handleWrite, this)); // FIXME: unsafe + std::bind(&Connector::handleWrite, this)); channel_->setErrorCallback( - std::bind(&Connector::handleError, this)); // FIXME: unsafe + std::bind(&Connector::handleError, this)); // channel_->tie(shared_from_this()); is not working, // as channel_ is not managed by shared_ptr @@ -146,7 +147,7 @@ int Connector::removeAndResetChannel() channel_->remove(); int sockfd = channel_->fd(); // Can't reset channel_ here, because we are inside Channel::handleEvent - loop_->queueInLoop(std::bind(&Connector::resetChannel, this)); // FIXME: unsafe + loop_->queueInLoop(std::bind(&Connector::resetChannel, this)); return sockfd; }