title: passing a string variable to boost::ASIO reader/handlers sause seg fault
body: <p>I was trying to modify a boost::asio example when I hit this problem:
  I simplified the code for easy reading(u'll see a lot of <code>cout</code>s for more help):</p>
  
  <p>-Somewhere in my code, <strong>I initiate an <code>async_read</code>:</strong></p>
  
  <pre><code>class WhoAreYouProtocol
  {
  public:
      std::string ID; //json string containing ID of the client
  private:
      void WhoAreYou_handler(const boost::system::error_code&amp; e,session_ptr sess) {
  
        ID.resize(1);//just for testing
        ID = "A";
        std::cout &lt;&lt; "1.Address of String1 " &lt;&lt; &amp;ID &lt;&lt; " value[" &lt;&lt; ID &lt;&lt; "] size " &lt;&lt; ID.size() &lt;&lt; std::endl; 
      sess-&gt;async_read(ID,
              boost::bind(&amp;WhoAreYouProtocol::WhoAreYou_response_handler, this,
                      boost::asio::placeholders::error, boost::ref(ID), sess));
  }
  
  void WhoAreYou_response_handler(const boost::system::error_code&amp; e, std::string &amp;ID_, session_ptr sess) {//...  }
  </code></pre>
  
  <p>};</p>
  
  <p><strong>Then, the program should do:</strong></p>
  
  <ol>
  <li>first, <strong><em>read</em></strong> the size of the main buffer     </li>
  <li><em><strong>resize</em></strong> a string variable to the size of incoming data</li>
  <li><em><strong>read</em></strong> the main buffer into the string variable</li>
  </ol>
  
  <p>here is the code:
  <strong>Read</strong></p>
  
  <pre><code>class session
  {
  public:
  
    template &lt;typename Handler&gt;
    void async_read(std::string&amp; t, Handler handler)
    {
        t.resize(2);//just testing
        std::cout &lt;&lt; "Address of String2 " &lt;&lt; &amp;t &lt;&lt; std::endl;
        std::cout &lt;&lt; "String2 size " &lt;&lt; t.size() &lt;&lt; std::endl;
        t = "AB";
        std::cout &lt;&lt; "2.Address of String " &lt;&lt; &amp;t &lt;&lt; " value[" &lt;&lt; t &lt;&lt; "] size " &lt;&lt; t.size() &lt;&lt; std::endl;
      // Issue a read operation to read exactly the number of bytes in a header.
      void (session::*f)(const boost::system::error_code&amp;,std::string&amp;, boost::tuple&lt;Handler&gt;) = &amp;session::handle_read_header&lt;Handler&gt;;
      boost::asio::async_read(socket_, boost::asio::buffer(inbound_header_),boost::bind(f,this, boost::asio::placeholders::error, boost::ref(t),boost::make_tuple(handler)));
  
  
    }
  </code></pre>
  
  <p><strong>Resize and read the main buffer:</strong></p>
  
  <pre><code>  /// Handle a completed read of a message header. The handler is passed using
    /// a tuple since boost::bind seems to have trouble binding a function object
    /// created using boost::bind as a parameter.
    template &lt;typename Handler&gt;
    void handle_read_header(const boost::system::error_code&amp; e,std::string&amp; t, boost::tuple&lt;Handler&gt; handler)
    {
        t.resize(3);//and again, just testing
        t = "ABC";
        std::istringstream is(std::string(inbound_header_, header_length));
        std::size_t inbound_data_size = 0;
        inbound_data_.resize(inbound_data_size);
  
        void (session::*f)(const boost::system::error_code&amp;,std::string&amp;, boost::tuple&lt;Handler&gt;) = &amp;session::handle_read_data&lt;Handler&gt;;
        boost::asio::async_read(socket_, boost::asio::buffer(inbound_data_),boost::bind(f, this,boost::asio::placeholders::error, boost::ref(t), handler));
  
    }
  </code></pre>
  
  <p><strong>and Finally the last problem handler :</strong></p>
  
  <pre><code>  /// Handle a completed read of message data.
    template &lt;typename Handler&gt;
    void handle_read_data(const boost::system::error_code&amp; e,std::string&amp; t, boost::tuple&lt;Handler&gt; handler)
    {
        std::cout &lt;&lt; "4.Address of String " &lt;&lt; &amp;t  &lt;&lt; std::endl;
        //any of the following lines crashes!!!!
        std::cout &lt;&lt; "4. value[" &lt;&lt; t &lt;&lt; "] size " &lt;&lt; t.size() &lt;&lt; std::endl;
  //    t.resize(4); //crash!!!
          std::string archive_data(&amp;inbound_data_[0], inbound_data_.size());
          t = archive_data;//my target is to reach here but it crashes!!!!
    }
  
  private:
    /// The size of a fixed length header.
    enum { header_length = 8 };
  
    /// Holds an outbound header.
    std::string outbound_header_;
  
    /// Holds the outbound data.
    std::string outbound_data_;
  
    /// Holds an inbound header.
    char inbound_header_[header_length];
  
    /// Holds the inbound data.
    std::vector&lt;char&gt; inbound_data_;
  };
  </code></pre>
  
  <p><em>In short, I passed a <code>string</code> in a few from the initial <code>async_read</code> down to the final handler(as you can see it took the few hops).</em></p>
  
  <p><strong>PROBLEM: In the last handler, almost every operation on the string crashed (cout, assign,resize)</strong></p>
  
  <p>I will value your kind time to go over the code and let me know where I have done something wrong? ... and how to solve it :)</p>
  
  <p>thanks very much</p>