On Fri, Jan 25, 2013 at 4:42 AM, huvcbo <christer.borgqvist10@bredband.net> wrote:
Hi all,

I have  problem with HTTP Server3 example,
in connection::handle_write i have a dedline_timer with a
async_wait.
but before the handle_timed execute the connection_ptr goes out of scope, why?


Appriciate any help.

Christer

The boost HTTP Server 3 in file connection.cpp
in server.hpp
boost::shared_ptr<connection> connection_ptr;
server.cpp
        void server::handle_accept(const boost::system::error_code& e)
        {
            if (!e)
            {
                new_connection_->start();
                new_connection_.reset(new connection(io_service_));
                acceptor_.async_accept(new_connection_->socket(),
                    boost::bind(&server::handle_accept, this,
                        boost::asio::placeholders::error));
            }
        }

in connection.cpp

void connection::handle_write(const boost::system::error_code& e)
{
    if (!e)
    {
       const boost::shared_ptr<boost::asio::deadline_timer> t(new
       boost::asio::deadline_timer(*io_service_,
       boost::posix_time::seconds(15)));
    }
}

void connection::handle_timed(
      const boost::system::error_code& ec,
        const boost::shared_ptr<boost::asio::deadline_timer> t)
{
     syslog(LOG_INFO, "handle_timed: %s", ec.message().c_str());

}


_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi,

I usually use timer as follows

struct connection : enable_shared_from_this<connection>
  // ...
private:
  socket socket_;
  mutable_buffer buffer_;
  deadline_timer timer_; // According to your design, you may put timer and buffer in some other classes. e.g. session
                                   // I put the timer here for brevity. 
};

void connection::read(...) 
{
  timer_.expires_from_now(posix_time::seconds(5));
  timer_.async_wait(bind(&connection::handle_timeout, shared_from_this(), _1)); // bind will increase ref cnt of  this connection
  socket_.async_read_some(buffer_, bind(&connection::handle_read, _1, _2));
}

void connection::handle_timeout(error_code const & err)
{
  if(!err) { // timeout
    socket_.close(); // cause handle_read receive error_code 'Operation Canceled'
  }
}

Hope this helps.

Best,
Acer.