c++ - 将字符串变量传递给 boost::ASIO 读取器/处理程序会导致段错误

标签 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. resize一个字符串变量到传入数据的大小
  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_;
};

简而言之,我从最初的 async_read 传递了一个 string 到最终的处理程序(如您所见,它经过了几个跃点) .

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

我会珍惜你花时间检查代码并告诉我哪里做错了什么? ...以及如何解决它:)

非常感谢

最佳答案

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

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

关于c++ - 将字符串变量传递给 boost::ASIO 读取器/处理程序会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16233198/

相关文章:

c++ - OpenGL 1.x 显示带有纹理的 GL_QUADS,纯色不

c++ - HashMap 和 vector 很慢

c++ - 捕获 const this

c++ - boost绑定(bind)回调函数指针作为参数

c++ - 连续调用 boost::asio::read_async: 没有数据的回调

c++ - 使用 .DEF 创建 DLL 以定义函数名称

c++ - 如何使用 Boost IOStreams 的 Gzip 文件接口(interface)逐行读取?

c++ - boost asio 计时器不适用于阻塞读取调用

c++ - 提升 asio 优先级队列,从处理程序添加异步操作

c++ - boost::asio::high_resolution_timer 用法示例?