我使用升壓通過TCP與async_write
發送數據:當這被刪除時async_write是否會導致分段錯誤?
std::shared_ptr<std::vector<std::string>::iterator> iter = std::make_shared<std::vector<std::string>::iterator>(this->m_vDataToWrite.begin());
boost::asio::async_write(this->socket_, boost::asio::buffer(*message),
boost::asio::transfer_all(), boost::bind(&Session::writeHandler,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred(),
message,
iter
));
// ...
void Session::writeHandler(const boost::system::error_code &error, std::size_t bytes_transferred, std::shared_ptr<std::string> message, std::shared_ptr<std::vector<std::string>::iterator> it)
{
if(error)
{
std::cout << "Write handler error: " << error.message() << std::endl;
this->disconnect();
delete this;
return;
}
//....
請注意,在寫處理程序delete this;
。
因此,當發送數據時發生錯誤時,會話將斷開連接,然後自行刪除。但是,writeHandler
已返回後出現分段錯誤。難道是因爲我刪除this
,雖然它被捕獲在async_write
?
我知道,但是我不再在「我的」代碼中訪問它。所以我認爲可以在'writeHandler'返回後通過'async_write'訪問。 – Bobface