TcpClient和TcpConnection的生命周期问题

95 views
Skip to first unread message

elith...@gmail.com

unread,
Feb 7, 2017, 1:29:29 AM2/7/17
to Muduo network library
在使用muduo的过程中发现一个问题, TcpClient包含一个TcpConnectionPtr的数据成员, 用来保存建立好的连接. TcpClient的newConnection()函数里调用setCloseCallback设置了回调函数,

conn->setCloseCallback(
boost::bind(&TcpClient::removeConnection, this, _1)); // FIXME: unsafe

但是这个回调函数是TcpClient的成员函数, 而TcpConnection是由shared_ptr管理的, 如果TcpClient对象已经析构了, 但是TcpConnection对象还存在的情况下, 一旦连接断开了触发closecallback, 这时候就会产生问题了, 不知道这块是我理解的不对, 还是有意这么设计的?

Shuo Chen

unread,
Feb 7, 2017, 4:21:51 PM2/7/17
to muduo-...@googlegroups.com
一般来说,你应该保证 TcpClient 的生命期长于 TcpConnection 。

--
You received this message because you are subscribed to the Google Groups "Muduo network library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to muduo-library+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages