これは、例に基づいた私の Boost.Asio プロジェクトの SSCCE です。これまでバグを追跡するのに約1時間かかりました:
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
class Connection {
public:
Connection(boost::asio::io_service& io_service) : socket(io_service) {}
private:
boost::asio::ip::tcp::socket socket;
};
class Server {
public:
Server() : signal_monitor(io_service) {
signal_monitor.add(SIGINT);
signal_monitor.add(SIGTERM);
signal_monitor.async_wait(
boost::bind(&Server::handle_signal_caught, this)
);
}
void run() {
// comment out the next line and there's no segfault
connection.reset(new Connection(io_service));
io_service.run();
}
private:
void handle_signal_caught() {
io_service.stop();
}
boost::shared_ptr<Connection> connection;
boost::asio::io_service io_service;
boost::asio::signal_set signal_monitor;
};
int main(int argc, char **argv) {
Server server;
server.run();
return 0;
}
シグナル (ctrl+C) を送信すると、プログラムは適切にシャットダウンする代わりにセグメンテーション違反を起こします。私はこれを見て最後の30分を費やしましたが、なぜこれがセグメンテーション違反になるのかわかりません。どなたか問題を見つけることができますか?