title: Why does this Boost TCP socket work in one method and not in another?
body: <p>I want to establish a socket connection in one method and use this connection in another method of a class. While in the first method (where I establish the connection) I can read and write from and to the socket as much as I want, in the second method I always get a <code>Bad file descriptor</code> error. <strong>Note that I use version <code>1.65.1</code> of Boost.</strong></p>
  <p>I created a small reproducible example (I added some logs for clarification):</p>
  <p>code.h</p>
  <pre><code>#include &lt;boost/asio.hpp&gt;
  
  using boost::asio::ip::tcp;
  
  class SocketManager {
      tcp::socket *sock;
      boost::asio::io_service *io_service;
      tcp::acceptor *acceptor;
  
      public:
          virtual void initialize();  // works
          virtual void evaluate();  // fails
  };
  </code></pre>
  <p>code.cc</p>
  <pre><code>#include &quot;code.h&quot;
  #include &lt;iostream&gt;
  #include &lt;boost/asio.hpp&gt;
  
  using boost::asio::ip::tcp;
  using namespace std;
  
  
  int main() {
      SocketManager sm;
      sm.initialize();
      sm.evaluate();
      return 0;
  }
  
  void SocketManager::initialize() {
      boost::asio::io_service _io_service;
      io_service = &amp;_io_service;
  
      tcp::acceptor a(*io_service, tcp::endpoint(tcp::v4(), 1337));
      acceptor = &amp;a;
  
      tcp::socket sock_(*io_service);
      sock = &amp;sock_;
  
      a.accept(*sock);
  
      char data[1024];
      boost::system::error_code error;
      size_t length = sock-&gt;read_some(boost::asio::buffer(data), error);
      if (error)
          throw boost::system::system_error(error);  // No Error here!
  
      boost::asio::write(*sock, boost::asio::buffer(data, length));
  
      cout &lt;&lt; io_service-&gt;stopped() &lt;&lt; endl;  // prints 0
      cout &lt;&lt; acceptor-&gt;is_open() &lt;&lt; endl;  // prints 1
      cout &lt;&lt; sock-&gt;is_open() &lt;&lt; endl;  // prints 1
  }
  
  void SocketManager::evaluate() {
      cout &lt;&lt; io_service-&gt;stopped() &lt;&lt; endl;  // prints 0
      cout &lt;&lt; acceptor-&gt;is_open() &lt;&lt; endl;  // prints 1
      cout &lt;&lt; sock-&gt;is_open() &lt;&lt; endl;  // prints 1
  
      char data[1024];
      boost::system::error_code error;
      size_t length = sock-&gt;read_some(boost::asio::buffer(data), error);
      if (error)
          throw boost::system::system_error(error);  // Error: &quot;Bad file descriptor&quot;
  }
  </code></pre>
  <p>Output of the code above when used with a client:</p>
  <pre><code>root@5531547d4baf:/cpp# ./a.out 
  0
  1
  1
  0
  1
  1
  terminate called after throwing an instance of 'boost::system::system_error'
    what():  Bad file descriptor
  Aborted
  </code></pre>
  <p>Note that the code that throws the error is the exact same code in both methods (in the first one it works without any error and in the second one it throws a <code>Bad file descriptor</code> error).</p>
  <p>I suspect that the <code>io_service</code> is no longer valid when switching to the new function, so I have already experimented with the associated <code>poll</code> and <code>run</code> functions without success.</p>
  <p>I also tried to replace the <code>boost::asio::buffer</code> with a <code>boost::asio::mutable_buffer</code> and a <code>boost::asio::mutable_buffer</code>. However, the exact same code works in the first method, so I suspect that the function calls are alright!</p>