将字符串变量传递给boost :: ASIO读取器/处理程序sause seg fault

时间:2013-04-26 09:33:59

标签: c++ boost boost-asio

当我遇到这个问题时,我试图修改一个boost :: asio示例: 我简化了代码以便于阅读(你会看到很多cout以获得更多帮助):

- 在我的代码的某处,我发起async_read

class WhoAreYouProtocol
{
public:
    std::string ID; //json string containing ID of the client
private:
    void WhoAreYou_handler(const boost::system::error_code& e,session_ptr sess) {

      ID.resize(1);//just for testing
      ID = "A";
      std::cout << "1.Address of String1 " << &ID << " value[" << ID << "] size " << ID.size() << std::endl; 
    sess->async_read(ID,
            boost::bind(&WhoAreYouProtocol::WhoAreYou_response_handler, this,
                    boost::asio::placeholders::error, boost::ref(ID), sess));
}

void WhoAreYou_response_handler(const boost::system::error_code& e, std::string &ID_, session_ptr sess) {//...  }

};

然后,程序应该:

  1. 首先, 读取 主缓冲区的大小
  2. 调整大小 字符串变量为传入数据的大小
  3. 读取 将主缓冲区转换为字符串变量
  4. 这是代码: 的

    class session
    {
    public:
    
      template <typename Handler>
      void async_read(std::string& t, Handler handler)
      {
          t.resize(2);//just testing
          std::cout << "Address of String2 " << &t << std::endl;
          std::cout << "String2 size " << t.size() << std::endl;
          t = "AB";
          std::cout << "2.Address of String " << &t << " value[" << t << "] size " << t.size() << std::endl;
        // Issue a read operation to read exactly the number of bytes in a header.
        void (session::*f)(const boost::system::error_code&,std::string&, boost::tuple<Handler>) = &session::handle_read_header<Handler>;
        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)));
    
    
      }
    

    调整并阅读主缓冲区:

      /// 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 <typename Handler>
      void handle_read_header(const boost::system::error_code& e,std::string& t, boost::tuple<Handler> 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&,std::string&, boost::tuple<Handler>) = &session::handle_read_data<Handler>;
          boost::asio::async_read(socket_, boost::asio::buffer(inbound_data_),boost::bind(f, this,boost::asio::placeholders::error, boost::ref(t), handler));
    
      }
    

    ,最后是最后一个问题处理程序:

      /// Handle a completed read of message data.
      template <typename Handler>
      void handle_read_data(const boost::system::error_code& e,std::string& t, boost::tuple<Handler> handler)
      {
          std::cout << "4.Address of String " << &t  << std::endl;
          //any of the following lines crashes!!!!
          std::cout << "4. value[" << t << "] size " << t.size() << std::endl;
    //    t.resize(4); //crash!!!
            std::string archive_data(&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<char> inbound_data_;
    };
    

    简而言之,我从最初的string向下传递了一个async_read到最终的处理程序(因为你可以看到它花了几个跃点)。 < / p>

    问题:在最后一个处理程序中,字符串上的几乎所有操作都崩溃了(cout,assign,resize)

    我会非常重视您的代码,让我知道我做错了什么? ......以及如何解决它:)

    非常感谢

1 个答案:

答案 0 :(得分:3)

async_read不知道缓冲区是什么,所以它假设它是一个缓冲区,它只能将数据放入,从而覆盖字符串的内部数据(并且它没有赢得&#39 ; t写入字符串中包含的缓冲区,该缓冲区很可能是指向堆上分配的内存的指针。这将导致未定义的行为和您遇到的崩溃。

使用Boost ASIO提供的缓冲,然后在回调中将该数据放入字符串中。