2016-04-17 4 views
0

をクラッシュ私は現在、ブーストを使用して、サーバーアプリケーションを作成しようとしています:: ASIO 2つの単純なことを行います。ブースト:: ASIO - async_acceptハンドラ内async_waitは、アプリケーション

  1. たら、クライアントの着信接続
  2. を受け入れます

    #define BOOST_ASIO_ENABLE_HANDLER_TRACKING 
    
    #include <WinSock2.h> 
    #include <Mswsock.h> 
    #include <boost/asio/io_service.hpp> 
    #include <boost/asio/ip/tcp.hpp> 
    #include <boost/bind.hpp> 
    #include <boost/date_time/posix_time/posix_time.hpp> 
    
    using namespace boost::asio; 
    using namespace boost::asio::ip; 
    
    void timerHandler(const boost::system::error_code& errorCode, deadline_timer* timer) { 
        timer->expires_at(timer->expires_at() + boost::posix_time::seconds(1)); 
        timer->async_wait(boost::bind(timerHandler, _1, timer)); 
    } 
    
    void acceptHandler(const boost::system::error_code &errorCode, io_service *ioService) { 
        deadline_timer timer(*ioService, boost::posix_time::seconds(1)); 
        timer.async_wait(boost::bind(timerHandler, _1, &timer)); 
    } 
    
    int main(int argc, char** argv) { 
        io_service ioService; 
        tcp::socket socket(ioService); 
        tcp::acceptor acceptor{ ioService, tcp::endpoint{ tcp::v4(), 12345 } }; 
        acceptor.listen(); 
        acceptor.async_accept(socket, boost::bind(acceptHandler, _1, &ioService)); 
        ioService.run(); 
        return EXIT_SUCCESS; 
    } 
    
    :クライアントは繰り返すboost::asio::deadline_timer

次のコードは、私の現在の試みを示し始める、受け入れられてきました

問題acceptHandlerで期待どおりに

タイマーは何とか動作しません。何とか2回取り消され、その上でエラーが発生し、最終的にアプリケーション全体がクラッシュします。

ハンドラのトラッキング出力

@asio|1460922050.075890|0*1|[email protected]_accept 
@asio|1460922051.153952|>1|ec=system:0 
@asio|1460922051.153952|1*2|[email protected]_wait 
@asio|1460922051.153952|1|[email protected] 
@asio|1460922051.153952|<1| 
@asio|1460922051.153952|>2|ec=system:995 
@asio|1460922051.153952|2|[email protected] 

質問

  1. acceptHandlerはハンドラトラッキング出力の4行目にdeadline_timerをキャンセルする原因は何?
  2. ハンドラートラッキング出力の6行目にエラー995がありますか?エラーメッセージは次のとおりです。I/O操作がスレッドの終了またはアプリケーションの要求ハンドラトラッキング出力の行7でdeadline_timerをキャンセルするtimerHandlerの原因は何

  3. のいずれかで中止されましたか?

答えて

2

timeracceptHandlerにスタック上に割り当てられ、timerHandlerが呼び出された時点で、したがって、有効ではありません。タイマーを動的に割り当てる必要があります。

また、両方のハンドラでエラーコードを確認する必要があります。これは、プログラムを終了したいときに特に重要です。cancelタイマー。

+1

どのような愚かな間違い。ありがとうございました。ここではスニペットの簡潔さのためにエラーチェックをしました。私は通常、すべてのハンドラでスイッチケースを扱います。 – chrisp

関連する問題