c ++助力导致崩溃

时间:2013-11-28 17:13:14

标签: c++ multithreading boost

我编写了一些带有c ++ 11特性和提升功能的多线程应用程序,但是我遇到了烦人的问题。

代码:

boost::asio::io_service io_service;
Client c(io_service, server->hostname.c_str(), server->port.c_str());
io_service.run(); //here is problem , it causes crashes

BT:

#0 0x414910 std::vector<boost::asio::ip::basic_resolver_entry<boost::asio::ip::tcp>, std::allocator<boost::asio::ip::basic_resolver_entry<boost::asio::ip::tcp> > >::operator[](this=0x0, __n=0) (/usr/include/c++/4.8/bits/stl_vector.h:771)
#1 0x4131d4 boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>::dereference(this=0x7fffdb7fd250) (/usr/include/boost/asio/ip/basic_resolver_iterator.hpp:183)
#2 0x411f24 boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>::operator*(this=0x7fffdb7fd250) (/usr/include/boost/asio/ip/basic_resolver_iterator.hpp:123)
#3 0x40f8c6 Client::handle_resolve(this=0x7fffdb7fd800, err=..., endpoint_iterator=...) (*******Client.h:48)
#4 0x41c21f boost::_mfi::mf2<void, Client, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >::operator() (this=0x7fffdb7fd450, p=0x7fffdb7fd800, a1=..., a2=...) (/usr/include/boost/bind/mem_fn_template.hpp:280)
#5 0x41bd3b boost::_bi::list3<boost::_bi::value<Client*>, boost::arg<1> (*)() (/usr/include/boost/bind/bind.hpp:392)
#6 0x41b74e boost::_bi::bind_t<void, boost::_mfi::mf2<void, Client, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Client*>, boost::arg<1> (*)() (/usr/include/boost/bind/bind_template.hpp:102)
#7 0x41adaf boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf2<void, Client, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Client*>, boost::arg<1> (*)() (/usr/include/boost/asio/detail/bind_handler.hpp:127)
#8 0x419fe1 boost::asio::asio_handler_invoke<boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf2<void, Client, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Client*>, boost::arg<1> (*)() (/usr/include/boost/asio/handler_invoke_hook.hpp:64)
#9 0x4191d8 boost_asio_handler_invoke_helpers::invoke<boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf2<void, Client, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Client*>, boost::arg<1> (*)() (/usr/include/boost/asio/detail/handler_invoke_helpers.hpp:37)
#10 0x41776c    boost::asio::detail::resolve_op<boost::asio::ip::tcp, boost::_bi::bind_t<void, boost::_mfi::mf2<void, Client, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Client*>, boost::arg<1> (*)() (/usr/include/boost/asio/detail/resolve_op.hpp:112)
#11 0x409df4    boost::asio::detail::task_io_service_operation::complete(this=0x7fffc8000db0, owner=..., ec=..., bytes_transferred=0) (/usr/include/boost/asio/detail/task_io_service_operation.hpp:37)
#12 0x40c4f7    boost::asio::detail::task_io_service::do_run_one(this=0x7fffc8000910, lock=..., this_thread=..., ec=...) (/usr/include/boost/asio/detail/impl/task_io_service.ipp:384)
#13 0x40bf27    boost::asio::detail::task_io_service::run(this=0x7fffc8000910, ec=...) (/usr/include/boost/asio/detail/impl/task_io_service.ipp:153)
#14 0x40c8a7    boost::asio::io_service::run(this=0x7fffdb7fdbc0) (/usr/include/boost/asio/impl/io_service.ipp:59)
#15 0x41d9a3    some_function(server=0x67e0b0) (*********Server.cpp:17)

Client.h

 void handle_resolve(const boost::system::error_code& err,
                            tcp::resolver::iterator endpoint_iterator)
        {
            tcp::endpoint endpoint = *endpoint_iterator; //48 line
            ip_ = endpoint.address().to_string();
            if (!err)
            {
                boost::asio::async_connect(socket_, endpoint_iterator,
                                           boost::bind(&Client::handle_connect, this,
                                                       boost::asio::placeholders::error));
            }

刚编辑并添加了客户端部分。

2 个答案:

答案 0 :(得分:1)

查看堆栈跟踪,问题出在Client::handle_resolve的{​​{1}}第48行。

我最好的猜测是你要解除引用迭代器而不检查它是否有效。也许您可以发布该功能,如果您在查看时错误不明显。

UPDATE :我的猜测是正确的:在检查操作是否成功之前,您要取消引用迭代器。如果没有,则迭代器将不会被解除引用。

移动Client.h块内的前两行。

答案 1 :(得分:0)

由于我不详细了解io_service,我不得不猜一点。你在哪个平台上使用?在boost 1.41上我们遇到的问题是在QNX中没有正确检测到自动多线程功能,因此可能没有设置BOOST_ASIO_HAS_THREADS。这导致使用null_mutex而不是POSIX或Windows互斥锁。由于std :: vector本身不是线程安全的,因此可能导致具有segfault的竞争条件。