linyacool / WebServer

A C++ High Performance Web Server
MIT License
7.77k stars 2.1k forks source link

关于HttpData对象生命周期的问题 #66

Closed Hcoleyh closed 4 years ago

Hcoleyh commented 4 years ago

这是Server::handNewconn()函数中的代码, 这里的req_info被拷贝到EventLooppendingFunctors_中.

    shared_ptr<HttpData> req_info(new HttpData(loop, accept_fd));
    req_info->getChannel()->setHolder(req_info);
    loop->queueInLoop(std::bind(&HttpData::newEvent, req_info));

在函数EventLoop::doPendingFunctors运行完function所绑定的Httpdata::newEventfunctors被析构, 那上面的req_info所指的对象在这里就被析构了吗? Httpdata析构函数close(fd), 那之后的请求怎么处理的

void EventLoop::doPendingFunctors() {
  std::vector<Functor> functors;
  callingPendingFunctors_ = true;

  {
    MutexLockGuard lock(mutex_);
    functors.swap(pendingFunctors_);
  }

  for (size_t i = 0; i < functors.size(); ++i) functors[i]();
  callingPendingFunctors_ = false;
}
linyacool commented 4 years ago

@Hcoleyh 继续从HttpData::newEvent流程开始看的话,会绑定定时器的,最后被定时器管理起来

Hcoleyh commented 4 years ago

感谢🙏, 第一遍看代码时忽略了所有timer相关的代码.