问题描述
我正在使用Boost Asio库。我在使用boost :: async_read()操作时遇到困难。我正在这样使用。服务器asio读取操作的示例代码如下。
void HandleClient::StartHandling()
{
auto self(shared_from_this());
asio::async_read_until(m_Sock,m_Request,DELIMITER,[this,self](
const boost::system::error_code& a_Ec,std::size_t a_BytesRead)
{
onRequestReceived(a_Ec,a_BytesRead);
});
}
void HandleClient::onRequestReceived(const system::error_code &a_Ec,std::size_t a_BytesRead)
{
if(a_Ec)
{....}
auto self(shared_from_this());
if(a_BytesRead > 0)
{
auto response = ProcessData(m_Request);
if(!response.empty())
{
m_Data = response + DELIMITER;
asio::async_write(m_Sock,asio::buffer(m_Data),self](boost::system::error_code& a_Ec,std::size_t
a_Bytestransferred))
{
response_sent(a_Ec,a_Bytestransferred);
});
}
asio::async_read_until(
m_Sock,self](const boost::system::error_code& a_Ec,std::size_t
a_BytesRead)
{
onRequestReceived(a_Ec,a_BytesRead);
});
}
经过一轮迭代后它崩溃了。崩溃是随机的。客户端代码采用同步调用的形式。我希望这不是问题。我收到调试断言失败消息的错误消息:
Debug assertion failed Program:xx/vc/tools/msvc/14.16.27023/include/xstring Line 1427. Expression cannot deference string iteration because the iterator was invalidated (e.g. reallocation occurred,or the string was destroyed)
解决方法
我们不知道事物的生命是什么。
如果我们假设m_Data
是std::string
的成员,并且是使用std::enable_shared_from_this
的类的成员,则对明显的错别字取模:
auto self(shared_from_this));
就寿命而言,一切似乎应该没事。但是,由于您似乎没有按照响应缓冲区(m_Data
?来同步任何内容,因此可以想象下一个onRequestReceived
最终再次调用相同的代码,可能会覆盖{{ 1}},并在完全写入之前的回复之前添加新的回复。
很难从显示为例如m_Data
和response
似乎没有任何明确的关系:
m_Data
总结观察结果
- 考虑一个执行请求的逻辑链仅在响应完成后(例如,当您执行
auto response = ProcessData(m_Request); asio::async_write(m_Sock,asio::buffer(m_Data),// where did `m_Data` come // from?! // response **looks** like a local variable which // is dangerous in async code (of course it might // be a [smart] pointer or something,but we can't // tell
时,它们是只读的。 )。 - 考虑用绞线序列化该类的所有操作(可能是连接?会话?)
另请参阅:Why do I need strand per connection when using boost::asio?
,@Sehe正确指出,由于下一个异步调用在上一个完成之前,缓冲区已被覆盖。当缓冲区生存期出现问题时,通常会发生调试断言。程序员负责将缓冲区保持在范围内,直到异步调用完成为止。 我的问题的更正代码是:
void Connection::onRequestReceived(
const system::error_code &a_Ec,std::size_t a_BytesRead)
{
auto self(shared_from_this());
if (a_Ec)
{
putErrorLog(getClientInfo(),a_Ec.value(),a_Ec.message(),__FUNCTION__,__LINE__);
// Close on Client disconnect,Need to handle errors
onFinish();
return;
}
// Dispatch the request
if (a_BytesRead > 0)
{
// Process received message
auto response = ProcessRequest(m_Request);
// if the response is not an empty string,// send it back to the client as a response
if (!response.empty())
{
m_Data = response + DELIMITER;
asio::async_write(
*m_Sock.get(),asio::buffer(m_Data,m_Data .size()),m_Strand.wrap( [this,self](
const boost::system::error_code& a_Ec,std::size_t a_BytesTransferred)
{
onResponseSent(a_Ec,a_BytesTransferred);
}));
}
}
else
{
auto self(shared_from_this());
asio::async_read_until(
*m_Sock.get(),m_Request,DELIMITER,m_Strand.wrap([this,self](const
boost::system::error_code& a_Ec,std::size_t a_BytesRead)
{
onRequestReceived(a_Ec,a_BytesRead);
}));
}
}
void Connection::onResponseSent(const system::error_code &a_Ec,std::size_t
a_BytesTransferred)
{
if (a_Ec)
{
putErrorLog(getClientInfo(),__LINE__);
}
else
{
auto self(shared_from_this());
asio::async_read_until(
*m_Sock.get(),self](const boost::system::error_code&
a_Ec,a_BytesRead);
}));
}
}