std::shared_ptr<TcpClient> EventLoopGroup::creatTcpClient(const std::string &host, const std::string &service) { std::shared_ptr<TcpClient> tcpClient = std::make_shared<TcpClient>(); std::weak_ptr<TcpClient> weakTcpClient = tcpClient; Resolver::async_resolve( host, service, [this, weakTcpClient](std::list<Endpoint> eps) { if (weakTcpClient.expired() || eps.empty()) return; auto tcpClient = weakTcpClient.lock(); SockPtr sockPtr = std::make_shared<Socket>(eps.front()); sockPtr->connect(); EventLoop *loop = robinLoop1(sockPtr->fd()); tcpClient->setEventLoop(loop); tcpClient->setSocket(sockPtr); // BUG tcpClient与当前线程不在一个线程上时,可能会引起内存错误 // tcpClient->start(); // FIX if (loop == EventLoop::curr_thread_loop()) { tcpClient->start(); } else { loop->runEventHandler([tcpClient] { tcpClient->start(); }); } }); return tcpClient; }