当我运行此代码时,我得到 std::bad_functon_call 异常。我无法弄清楚这个异常的原因是什么。它是由receiveCallback 中的async_receive 抛出的。receiveCallback 在被调用之前是否从内存中清除?
//callback on connection accepted
std::function<void(const boost::system::error_code& error, tcp::socket* socketPtr)> acceptCallback =
[this, onMessageReceivedCallback, acceptCallback](const boost::system::error_code& error, tcp::socket* socketPtr)
{
cout<<"accept: "<<error.message()<<endl;
const int bufferSize = 100;
char* message = new char[bufferSize];
//callback on message received
std::function<void(const boost::system::error_code& error,std::size_t bytes_transferred)> receiveCallback =
[message, bufferSize, socketPtr, onMessageReceivedCallback, receiveCallback](const boost::system::error_code& error,std::size_t bytes_transferred)
{
onMessageReceivedCallback(message, bytes_transferred);
socketPtr->async_receive(
boost::asio::buffer(message, bufferSize),
receiveCallback);
};
socketPtr->async_receive(
boost::asio::buffer(message, bufferSize),
receiveCallback);
//create socket for the next connection
socketPtr = new tcp::socket(io_service_);
//continue accepting connections
acceptor_.async_accept(*socketPtr, std::bind(acceptCallback, std::placeholders::_1, socketPtr));