このコードを実行すると、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));