title: Boost.Asio TCP moved-to socket destructor not enough to cleanly close?
body: <p>Consider this test program :</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;boost/asio/io_service.hpp&gt;
  #include &lt;boost/asio/ip/tcp.hpp&gt;
  #include &lt;functional&gt;
  #include &lt;iostream&gt;
  
  static void callback (boost::asio::ip::tcp::socket &amp;&amp; socket)
  {
      //boost::asio::ip::tcp::socket new_socket = std::move(socket);
      std::cout &lt;&lt; "Accepted" &lt;&lt; std::endl;
  }
  
  static void on_accept (boost::asio::ip::tcp::acceptor &amp;  acceptor,
                         boost::asio::ip::tcp::socket &amp;    socket,
                         boost::system::error_code const &amp; error)
  {
      if (error)
      {
          std::cerr &lt;&lt; error &lt;&lt; ' ' &lt;&lt; error.message() &lt;&lt; std::endl;
          return ;
      }
  
      callback(std::move(socket));
      acceptor.async_accept
      (
          socket,
          std::bind(on_accept, std::ref(acceptor), std::ref(socket), std::placeholders::_1)
      );
  }
  
  int main ()
  {
      boost::asio::io_service        service;
      boost::asio::io_service::work  work { service };
      boost::asio::ip::tcp::acceptor acceptor { service };
      boost::asio::ip::tcp::socket   socket { service };
  
      boost::asio::ip::tcp::endpoint endpoint { boost::asio::ip::tcp::v4(), 5555 };
      boost::system::error_code      ec;
  
      using socket_base = boost::asio::socket_base;
      auto option = socket_base::reuse_address { false };
  
      if (acceptor.open(endpoint.protocol(), ec) ||
          acceptor.set_option(option, ec) ||
          acceptor.bind(endpoint, ec) ||
          acceptor.listen(socket_base::max_connections, ec) ||
          acceptor.is_open() == false)
          return 1;
  
      acceptor.async_accept
      (
          socket,
          std::bind(on_accept, std::ref(acceptor), std::ref(socket), std::placeholders::_1)
      );
  
      service.run();
  }
  </code></pre>
  
  <p>When I connect a client to it, I get an error :</p>
  
  <blockquote>
    <p>Accepted<br>
    system:1 Incorrect function</p>
  </blockquote>
  
  <p>(The <code>on_accept()</code> function is called with an error code when the <code>socket</code> object from the <code>callback()</code> function is destroyed).</p>
  
  <p>Also, the client is not disconnected at all.</p>
  
  <p>If I uncomment the line in the <code>callback()</code> function, everything works fine, no error message and the client is disconnected as expected.</p>
  
  <p>Now for the environment settings, I'm under Windows 8.1, using a MinGW-w64 v4.9.2 compiler with Boost.Asio v1.58.0 compiled with that same compiler.</p>
  
  <p>The command line used to compile the file is as follow :</p>
  
  <pre><code>$ g++ -std=c++14 -IC:/C++/boost/1.58.0 main.cpp -LC:/C++/boost/1.58.0/lib -lboost_system-mgw49-mt-1_58 -lwsock32 -lws2_32 -o test.exe
  </code></pre>
  
  <p>Note that using Boost 1.57.0 results in the same behavior.</p>
  
  <p>I can also remove the commented line completely, and then use this :</p>
  
  <pre><code>static void callback (boost::asio::ip::tcp::socket &amp;&amp; socket)
  {
      std::cout &lt;&lt; "Accepted" &lt;&lt; std::endl;
      socket.shutdown(socket.shutdown_both);
      socket.close();
  }
  </code></pre>
  
  <p>And the program will behave correctly too.</p>
  
  <hr>
  
  <p>So, how come I need to add extra steps to not get an error here ? IIRC this behavior wasn't there a couple of months ago when I first tried that program.</p>