0

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

- 在我的代码中,我启动了一个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. 将主缓冲区读入字符串变量

这是代码: 阅读

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到最终的处理程序(如您所见,它花了几跳)。

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

我会珍惜您的宝贵时间来检查代码并让我知道我在哪里做错了什么?...以及如何解决它:)

非常感谢

4

1 回答 1

3

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

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

于 2013-04-26T09:39:59.477 回答