title: C++ Boost.Asio Segmentation Fault on async_write while accessing write-method through shared_ptr
body: <p>I created a static map that holds several Sessions of connected clients.</p>
  
  <pre><code>std::map&lt;std::string, std::shared_ptr&lt;Session&gt;&gt; Communication::m_appSockets;
  </code></pre>
  
  <p>The Listener that accepts incomming clients is implemented in the Communication class.</p>
  
  <pre><code>class Communication : public std::enable_shared_from_this&lt;Communication&gt;
  {
  private:
    boost::asio::io_context m_ioc;
    boost::asio::io_context::work m_work;
    boost::asio::streambuf m_buffer;
    boost::asio::ip::tcp::acceptor m_acceptor;
    std::thread m_senderThread;
    std::thread m_ioThread;
  
  public:
    static std::map&lt;std::string, std::shared_ptr&lt;Session&gt;&gt; m_appSockets;
  
    Communication(uint16_t t_port);
  
    void accept();
  
    void doAccept();
  
    void senderThread();
  };
  </code></pre>
  
  <p>After accepting a client the method "doAccept" creates a session object and moves the socket like this</p>
  
  <pre><code>  m_acceptor.async_accept(
      [this](boost::system::error_code t_ec, boost::asio::ip::tcp::socket t_socket) {
        if (!t_ec)
        {
              m_appSockets.emplace(std::pair&lt;std::string, std::shared_ptr&lt;Session&gt;&gt;(
                "app0", std::make_shared&lt;Session&gt;(std::move(t_socket))));
              m_appSockets["app0"]-&gt;start();
        }
        accept();
      });
  </code></pre>
  
  <p>Session.h looks like this:</p>
  
  <pre><code>class Session : public std::enable_shared_from_this&lt;Session&gt;
  {
  private:
    boost::asio::ip::tcp::socket m_socket;
    boost::asio::streambuf m_buffer;
  
  public:
  
    Session(boost::asio::ip::tcp::socket t_socket);
  
    void write(std::string &amp;t_msg);
    void doWrite(std::string &amp;t_msg);
    void start();
  ...
  };
  </code></pre>
  
  <p>void start() is used for starting the async read on the socket, which is working fine.
  A session object is created this way:</p>
  
  <pre><code>Session::Session(boost::asio::ip::tcp::socket t_socket) : m_socket(std::move(t_socket))
  {}
  </code></pre>
  
  <p>What I need to do for my implementation is to access the write-method of session through the shared_ptr in the map of Communication.h.
  I tried it the following way</p>
  
  <pre><code>void Communication::senderThread()
  {
    for (;;)
    {
      .... 
      //blocking until queue holds a message
      std::string buf = *message from queue*//pseudo code
      m_appSockets["app0"].get()-&gt;write(buf);
  
    }
  }
  </code></pre>
  
  <p>A senderthread blocks until a message is available in a queue which will be forwarded to the write method of session </p>
  
  <p>The write-method can be called but as soon as i try an operation on any member of the session it gives me a segmentation fault:</p>
  
  <pre><code>void Session::write(std::string &amp;t_msg)
  {
  //here it crashes
    m_socket.get_executor().context().post(std::bind(&amp;Session::doWrite, shared_from_this(), t_msg));
  }
  
  void Session::doWrite(std::string &amp;t_msg)
  {
    boost::asio::async_write(
      m_socket, boost::asio::buffer(t_msg),
      std::bind(&amp;Session::onWrite, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
  }
  </code></pre>
  
  <p>It feels like the Session object runs out of scope as soon as I enter its method. I have tried creating dummy members in Session which all gave the same segmentation fault when accessing them. 
  Am I getting the shared_ptr/object lifetime wrong at some point?</p>
  
  <p>Thank you very much in advance.</p>
  
  <p>EDIT 1:
  Running gdb ./programm.out core gave me this:</p>
  
  <blockquote>
    <p>Thread 2 "programm.out" received signal SIGSEGV, Segmentation fault.
    [Switching to Thread 0x7ffff58f1700 (LWP 5651)] 0x0000555555605932 in
    Session::write (this=0x0, 
        t_msg="{\"destination\":\"app0\"}")
        at /usr/Sources/Session.cpp:58 58   std::cout &lt;&lt; dummyMember &lt;&lt; std::endl;</p>
  </blockquote>
  
  <p>I added a member to Session (int dummyMember{5};). </p>
  
  <p>How can it be that this is pointing to 0x0?</p>