在大多数情况下,我的程序运行良好,但有时会崩溃。如果我在运行中暂停程序,它也会崩溃。关于为什么的任何见解将不胜感激!我认为这可能是由于async_read_some在实际执行之前被多次调用了。
Main.cpp:
while(true)
{
sensor->update();
if (sensor->processNow == 1)
{
sensor->process(4);
sensor->processNow = 0;
sensorReadyForUpdate = 1;
}
}
构造函数:
sensorHandler::sensorHandler(std::string host, int port, std::string name) :
socket_(ioservice_),
sensorAddress_(boost::asio::ip::address::from_string(host), port),
dataRequested_(false),
dataReady_(false)
{
}
更新功能:
bool sensorHandler::update()
{
ioservice_.poll_one();
if (inOperation == false)
{
inOperation = true;
socket_.async_read_some(boost::asio::buffer(receiveBuffer, receiveBuffer.size()), boost::bind(&sensorHandler::receiveCallback, this, _1, _2));
return success;
}
}
接收回调函数:
bool sensorHandler::receiveCallback(const boost::system::error_code& error, std::size_t bytes_transferred)
{
std::cout << "success - in receiveCallBack" << std::endl;
processNow = 1;
inOperation = false;
}
包括:
#include "sensorHandler.h"
#include <boost\bind.hpp>
#include <boost\asio\write.hpp>
#include <iostream>
#include <windows.h>
头文件:
class sensorHandler
{
public:
sensorHandler(std::string host, int port, std::string name);
~sensorHandler();
bool connect();
bool update();
boost::array<char, 400000> receiveBuffer; // was 50000
}