Boost Asio tcp::acceptor access violation exception
Asked Answered
D

2

6

I'm trying to create a tcp::acceptor and a libtorrent::session side by side, but I get an access violation exception when waiting for input from std::cin. The access violation occurs in the Boost IOCP implementation as shown by the stack trace.

  • Windows 8.1
  • MSVC 12.0 (Visual Studio 2013)
  • boost v1.57.0
  • libtorrent-rasterbar v1.0.3

A minimal example which lets me reproduce the issue,

#include <boost/asio.hpp>
#include <libtorrent/session.hpp>

int main(int argc, char *argv[])
{
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::acceptor acceptor(io_service);

    libtorrent::session s;

    std::string line;
    std::getline(std::cin, line); // <----- trigger

    return 0;
}

The stack trace is

lt_test.exe!boost::asio::detail::win_iocp_socket_service_base::do_open(boost::asio::detail::win_iocp_socket_service_base::base_implementation_type & impl, int family, int type, int protocol, boost::system::error_code & ec) Line 300   C++
lt_test.exe!boost::asio::detail::win_iocp_socket_service<boost::asio::ip::tcp>::open(boost::asio::detail::win_iocp_socket_service<boost::asio::ip::tcp>::implementation_type & impl, const boost::asio::ip::tcp & protocol, boost::system::error_code & ec) Line 192  C++
lt_test.exe!boost::asio::socket_acceptor_service<boost::asio::ip::tcp>::open(boost::asio::detail::win_iocp_socket_service<boost::asio::ip::tcp>::implementation_type & impl, const boost::asio::ip::tcp & protocol, boost::system::error_code & ec) Line 144  C++
lt_test.exe!boost::asio::basic_socket_acceptor<boost::asio::ip::tcp,boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >::open(const boost::asio::ip::tcp & protocol, boost::system::error_code & ec) Line 310    C++
lt_test.exe!libtorrent::aux::session_impl::setup_listener(libtorrent::listen_socket_t * s, boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> ep, int & retries, bool v6_only, int flags, boost::system::error_code & ec) Line 2089    C++
lt_test.exe!libtorrent::aux::session_impl::open_listen_port(int flags, boost::system::error_code & ec) Line 2243  C++
lt_test.exe!libtorrent::aux::session_impl::init() Line 1258   C++
lt_test.exe!libtorrent::aux::session_impl::main_thread() Line 4880    C++
lt_test.exe!boost::_mfi::mf0<void,libtorrent::aux::session_impl>::operator()(libtorrent::aux::session_impl * p) Line 50   C++
lt_test.exe!boost::_bi::list1<boost::_bi::value<libtorrent::aux::session_impl *> >::operator()<boost::_mfi::mf0<void,libtorrent::aux::session_impl>,boost::_bi::list0>(boost::_bi::type<void> __formal, boost::_mfi::mf0<void,libtorrent::aux::session_impl> & f, boost::_bi::list0 & a, int __formal) Line 254   C++
lt_test.exe!boost::_bi::bind_t<void,boost::_mfi::mf0<void,libtorrent::aux::session_impl>,boost::_bi::list1<boost::_bi::value<libtorrent::aux::session_impl *> > >::operator()() Line 21   C++
lt_test.exe!boost::asio::detail::win_thread::func<boost::_bi::bind_t<void,boost::_mfi::mf0<void,libtorrent::aux::session_impl>,boost::_bi::list1<boost::_bi::value<libtorrent::aux::session_impl *> > > >::run() Line 117 C++
lt_test.exe!boost::asio::detail::win_thread_function(void * arg) Line 118 C++
[External Code] 
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  

Removing the acceptor makes the access violation exception disappear.

What causes this?

Duodecillion answered 8/1, 2015 at 20:53 Comment(1)
Does libtorrent influence things? Because if it does, that's the lead. Look at what the session constructor doesSkullcap
P
1

I ran into the same problem. I was helped by adding BOOST_ASIO_ENABLE_CANCELIO in preprocessor definitions. Here you can find more information.

Prehensible answered 4/12, 2017 at 12:31 Comment(0)
D
0

I have run into the same issue, I had nothing wrong in my code, indeed using another constructor of acceptor worker fine, this was crashing to me:

this->acceptor = boost::make_shared<boost::asio::ip::tcp::acceptor>(io_service_, endpoint);

whereas this was not

this->acceptor = boost::make_shared<boost::asio::ip::tcp::acceptor>(io_service_, endpoint.protocol());

notice the protocol() call, I am using another constructor, surprisingly this worked ...

Director answered 17/10, 2018 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.