Re: [asio-users] Segfault on read_op
Brought to you by:
chris_kohlhoff
From: Benjamin R. <be...@bl...> - 2017-09-05 11:58:39
|
Hi asio users I took all your recommendations, read up on how asio works and rebuilt my networking class. I posted/wrapped every single operation on a socket into its corresponding strand (one strand per socket) and I used shared pointers for all lambdas and bound functions to make sure the object does not go out of scope. However, now I get segmentation faults. It seems to be caused by a pending async_read when the socket gets shut down by the remote machine (I would expect my receive callback to be called with EOF error, but I get a segfault instead). This happens before my callback is called, inside asio. Now, I am really at my wit's end. I do not know how I should debug this. I feel like I am using the asio API correctly: auto self(shared_from_this());//Important! Prevents this object from being deleted. strand.post( [this, self] { asio::async_read(self->socket, asio::buffer(header_buffer), strand.wrap(std::bind(on_received, shared_from_this(), std::placeholders::_1, std::placeholders::_2))); }); All the other async operations and calls on socket functions are posted and wrapped in a similar way. I also verified that the object still exists (the destructor has not run yet). It seems to be a multithreading problem though because I cannot reproduce it with just 1 worker thread. The function where it segfaults is inside asio\detail\call_stack.hpp, line 92: contains() And that function was called by asio\detail\impl\strand_service.hpp, line 54: dispatch<asio::detail::rewrapped_handler<asio::detail::binder2<asio::detail::read_op<asio::basic_stream_socket<asio::ip::tcp>, asio::mutable_buffers_1, asio::detail::transfer_all_t, asio::detail::wrapped_handler<asio::io_service::strand, std::_Bind<std::_Mem_fn<void (TCP_Socket_Impl::*)(const std::error_code&, unsigned int)>(std::shared_ptr<TCP_Socket_Impl>, std::_Placeholder<1>, std::_Placeholder<2>)>, asio::detail::is_continuation_if_running> >, std::error_code, unsigned int>, std::_Bind<std::_Mem_fn<vo... Does anyone have an idea? Thanks for your time. Cheers, Benjamin |