【问题标题】:Getting weird error while using boost.asio's free async_* functions使用 boost.asio 的免费 async_* 函数时出现奇怪的错误
【发布时间】:2013-07-12 13:02:24
【问题描述】:

我在我正在编写的应用程序中得到了这个代码 sn-p:

#include "ClientSession.hpp"
void ClientSession::start(void)
{
    auto Self(shared_from_this());

    //boost::asio::read(this->_Socket, boost::asio::buffer(this->_ReadBuffer));

    //this->_Socket.get_io_service().post(boost::bind(&ClientSession::on_read, shared_from_this(), boost::system::error_code::error_code(), 0));

    boost::asio::async_read(this->_Socket, boost::asio::buffer(this->_ReadBuffer), boost::bind(&ClientSession::on_read, Self, _1, _2));
}

void ClientSession::on_read(const boost::system::error_code& Err_, size_t Bytes_)
{
    try 
    {
        if(Err_)
        {
            if(!(Err_ == boost::asio::error::eof || Err_ == boost::asio::error::connection_aborted))
            {
                throw std::runtime_error(Err_.message());
            }
            return;
        }

    }
    catch(std::exception& Ex_)
    {
        this->_OutputHandler.write_error(Ex_.what());
    }
}

调用ClientSession::start的代码是:

void Application::start_accept()
{

    using namespace boost::asio;
    ip::tcp::endpoint Endpoint( ip::address::from_string(this->_Config.get_client_login_server().first), this->_Config.get_client_login_server().second );
    this->_ClientAcceptor.open(Endpoint.protocol());
    this->_ClientAcceptor.bind(Endpoint);
    this->_ClientAcceptor.listen();

    std::shared_ptr<ClientSession> Session = ClientSession::create_new(this->_AsioService, this->_OutputHandler);
    this->_ClientAcceptor.async_accept(Session->get_socket(), boost::bind(&Application::handle_accept, shared_from_this(), Session, _1));

}

如果我使用注释掉的行之一运行应用程序,它工作得很好(在 boost::asio::read 版本中,来自客户端的传输消息被写入读取缓冲区,并且在发布版本期间处理程序函数被调用而没有任何问题),但是如果尝试将我的应用程序与 async_read 部分一起使用,我的整个应用程序总是崩溃,没有调用完整的句柄或!甚至读取一个字节!,至少它看起来像 asio 的输出给我:

@asio|1373633560.834957|0*1|socket@000000BD603D46C0.async_accept @asio|1373633573.324144|>1|ex=system:0 @asio|1373633573.324144|1*2|socket@000000BD603DBAB8.async_receive

我将它调试到一个名为“select_reactor.ipp”的文件中,应用程序在该文件中尝试获取 boost::asio::detail::mutex::scoped_lock,看起来它在那里失败了,完整的代码方法是:

void select_reactor::start_op(int op_type, socket_type descriptor,
select_reactor::per_descriptor_data&, reactor_op* op,
bool is_continuation, bool)
{
  boost::asio::detail::mutex::scoped_lock lock(mutex_);

  if (shutdown_)
  {
    post_immediate_completion(op, is_continuation);
    return;
  }

  bool first = op_queue_[op_type].enqueue_operation(descriptor, op);
  io_service_.work_started();
  if (first)
    interrupter_.interrupt();
}

我不知道我可能做错了什么,但我不知道是什么,我希望有人能给我一个提示,我可以做些什么来解决这个错误。

基于我目前得到的cmets,我将添加更完整的代码sn-ps: 我的 io_service 在我的 Application 类中:

class Application
    : public std::enable_shared_from_this<Application>
{
typedef boost::asio::io_service Service;
typedef boost::asio::ip::tcp::acceptor Acceptor;

public:
    Application(void) 
        : _ClientAcceptor(_AsioService), _ApplicationRunning(true)
    {
    }

    ~Application(void);

    /*
     * This is the main method which starts the actual program routine
     * called in main()
     */
    int run(void);

private:
    /*
     * Invokes the first accept and then invokes via async. handle_accept, which invokes itself repeatedly
     */
    void start_accept(void);
    /*
     * handle_accept calls NewClient_->start to invoke the communication with this client 
     */
    void handle_accept(std::shared_ptr<ClientSession> NewClient_, const boost::system::error_code& Err_);

    void loop_for_user_input(void);
    bool handle_user_input(const std::string& Msg_);

    /*
     * This method invokes the threads that call io_service::run()
     */
    static void start_service(Service& AsioService_) { try { AsioService_.run(); } catch(const std::exception& Ex_) { std::cout << Ex_.what() << std::endl; } }

private:
    Configuration _Config;
    Service _AsioService;
    Acceptor _ClientAcceptor;
    OutputHandler _OutputHandler;

    bool _ApplicationRunning;
};

和运行方法:

int Application::run(void)
{
    try 
    {
        this->_Config.load();
        this->_OutputHandler.open(this->_Config.get_logs());

        this->start_accept();

        for(int i = 0; i < 4; i++)
        {
            std::thread t(start_service, std::ref(this->_AsioService));
            t.detach();
        }

        this->loop_for_user_input();

    }
    catch(const std::runtime_error& Ex_)
    {
        this->_OutputHandler.write_error(Ex_.what());
    }

    return 0;
}

【问题讨论】:

  • 你试过在valgrind下运行吗?
  • 您的 io_service 在哪里?有多少线程在调用 run()?我猜你在某个地方有一个对象生命周期问题,根据你包含的有限代码很难说。
  • 我在帖子中添加了更完整的代码源。我没有尝试在 valgrind 下运行它,因为我目前正在 windows 下开发此应用程序
  • 我发现了错误,这只是我的 _WIN32_WINNT 预处理器指令设置中的拼写错误,设置为 _WIN32_WINNT 0x0601(对于 Windows 7)解决了问题。
  • 请发布您的问题的答案,而不是直接编辑问题

标签: c++ boost boost-asio


【解决方案1】:

我发现了错误,这只是我的 _WIN32_WINNT 预处理器指令设置中的拼写错误,设置为 _WIN32_WINNT 0x0601(对于 Windows 7)解决了问题。

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2013-05-05
    • 2016-08-06
    • 2012-05-05
    • 2011-06-16
    • 1970-01-01
    • 2011-01-08
    • 2019-05-04
    • 2020-05-26
    相关资源
    最近更新 更多