关于Channel对象的生命期管理问题

42 views
Skip to first unread message

yinshen...@gmail.com

unread,
Dec 26, 2018, 2:34:56 PM12/26/18
to Muduo network library
想问下陈老师,将channel对象作为其owner对象的直接成员是否可行,而不使用smart pointer对其进行包装。以TcpConnection对象为例,只需要保证在Channel::handleEvent执行结束前,以及在TcpConnection::connectDestroyed执行结束前TcpConnection对象还存在即可,这样就可以保证在Poller::removeChannel的过程中其channel指针的有效性,为什么要使用unique_ptr对channel对象进行管理呢?

Shuo Chen

unread,
Dec 26, 2018, 2:38:59 PM12/26/18
to Muduo network library
muduo TcpConnection 这么做的初衷是避免将 Channel 的具体实现暴露给一般用户。


  // we don't expose those classes to client.
  std::unique_ptr<Socket> socket_;
  std::unique_ptr<Channel> channel_;
Reply all
Reply to author
Forward
0 new messages