Boost.Asio segfault, no idea why

不问归期 提交于 2019-12-04 08:00:26

I think I found out the issue. Note the declaration order of the members of Server:

boost::shared_ptr<Connection> connection;
boost::asio::io_service io_service;
boost::asio::signal_set signal_monitor;

Destruction order is done in the opposite order of declaration. This means that first signal_monitor, then io_service and finally connection get destroyed. But connection contains a boost::asio::ip::tcp::socket containing a reference to io_service, which got destroyed.

And indeed, this is pretty much what happening, and causes a segfault too:

int main(int argc, char **argv) {
    auto io_service = new boost::asio::io_service();
    auto socket = new boost::asio::ip::tcp::socket(*io_service);

    delete io_service;
    delete socket;

    return 0;
}

Declaring connection after io_service solves the issue.

Damn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!