From fca6d2328871fc6af75e215f89c3f1092ba5bb21 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 7 Feb 2024 22:55:52 +0800 Subject: [PATCH] Avoid event poller thread join and delete itself --- src/Network/Socket.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Network/Socket.cpp b/src/Network/Socket.cpp index 0587c0b0..261af94b 100644 --- a/src/Network/Socket.cpp +++ b/src/Network/Socket.cpp @@ -51,7 +51,14 @@ static SockException getSockErr(int sock, bool try_errno = true) { Socket::Ptr Socket::createSocket(const EventPoller::Ptr &poller_in, bool enable_mutex) { auto poller = poller_in ? poller_in : EventPollerPool::Instance().getPoller(); - return Socket::Ptr(new Socket(poller, enable_mutex), [poller](Socket *ptr) { poller->async([ptr]() { delete ptr; }); }); + std::weak_ptr weak_poller = poller; + return Socket::Ptr(new Socket(poller, enable_mutex), [weak_poller](Socket *ptr) { + if (auto poller = weak_poller.lock()) { + poller->async([ptr]() { delete ptr; }); + } else { + delete ptr; + } + }); } Socket::Socket(EventPoller::Ptr poller, bool enable_mutex)