title: boost::asio error "The I/O operation has been aborted..."
body: <p>I am receiving this error message</p>
  
  <p>"The I/O operation has been aborted because of either a thread exit or an application request"</p>
  
  <p>when using boost::asio::socket::async_read_some()</p>
  
  <p>What does the error mean?  What should I be looking for?</p>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>void tcp_connection::start()
  {
    printf("Connected to simulator\n");
  
    socket_.async_read_some(boost::asio::buffer(myBuffer,256),
        boost::bind(&amp;tcp_connection::read_sim_handler,this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
  }
  
  void tcp_connection::read_sim_handler(
                                    const boost::system::error_code&amp; error, // Result of operation.
                                    std::size_t len )         // Number of bytes read.
  {
  try {
  if (error == boost::asio::error::eof) {
      // Connection closed cleanly by peer.
      printf("Sim connection closed\n");
      return;
  } else if (error) {
      throw boost::system::system_error(error); // Some other error.  if( ! error ) 
  }
  
  socket_.async_read_some(boost::asio::buffer(myBuffer,256),
      boost::bind(&amp;tcp_connection::read_sim_handler,this,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
  }
   catch (std::exception&amp; e)
  {
  std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
  }
  }
  </code></pre>
  
  <p>When I replace the call to async_read_some() with read_some() in the start() method, everything works fine ( except the server blocks waiting for a message! )</p>
  
  <p>Following a comment i see that tcp_connection is going out of scope.  I copied the code from <a href="http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/tutorial/tutdaytime3.html" rel="nofollow">http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/tutorial/tutdaytime3.html</a>
   which says this: 
  "We will use shared_ptr and enable_shared_from_this because we want to keep the tcp_connection object alive as long as there is an operation that refers to it."
  I confess that I do not know what all that means.  So I have broken it somehow?</p>
  
  <p>Following further comments, the answer is </p>
  
  <pre><code>void tcp_connection::start()
  {
    printf("Connected to simulator\n");
  
    socket_.async_read_some(boost::asio::buffer(myBuffer,256),
        boost::bind(&amp;tcp_connection::read_sim_handler,
                shared_from_this(),
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
  }
  </code></pre>
  
  <p>Passing shared_from_this() rather than this employs the clever ( too clever? ) keep alive infrastructure established by the server code, even though the connection manager is not in scope, by normal means.  For technical details, see comments under accepted answer.</p>