gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:45:55 27 4
gpt4 key购买 nike

当我遇到这个问题时,我正试图修改一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com