Segmentation violation while destroying io_context
@igorpsoares commented on Sep 4, 2018, 7:18 PM UTC:
Hi, I am facing a segmentation violation on the destructor of a class that makes use of io_service. I am not sure whether this a bug within asio or if this is due to some misuse. I will be glad if someone have some advice on this.
Here is the exception Stack Trace:
Received signal: 11 - SIGSEGV. Segmentation violation
Thread: AsyncHttp-165596 - 140612727699200
Stack trace:
/ArbeitClick/ACServer/scserver: boost::asio::detail::timer_queue_set::get_all_timers(boost::asio::detail::op_queue<boost::asio::detail::scheduler_operation>&)()+0x21
/ArbeitClick/ACServer/scserver: boost::asio::detail::epoll_reactor::shutdown()()+0xdb
/ArbeitClick/ACServer/scserver: boost::asio::detail::service_registry::shutdown_services()()+0x31
/ArbeitClick/ACServer/scserver: boost::asio::execution_context::shutdown()()+0x1b
/ArbeitClick/ACServer/scserver: boost::asio::execution_context::~execution_context()()+0x19
/ArbeitClick/ACServer/scserver: boost::asio::io_context::~io_context()()+0x18
/ArbeitClick/ACServer/scserver: arbeit::httpclient::HttpClient::~HttpClient()()+0x14d
The io service is defined on my header file like this:
boost::asio::io_service ios;
On a method within the HttpClient class I start the thread like this:
httpThread = std::thread(boost::bind(&boost::asio::io_service::run, &ios));
In order to make sure I can destroy the HttpClient instance we call a method that checks if the ios service is stopped, which is the following:
bool HttpClient::isStopped()
{
return ios.stopped();
}
If the method above returns true I try to destroy the class instance. Here is the destructor:
HttpClient::~HttpClient()
{
if (httpThread.joinable())
httpThread.join(); //Waits until the service has stopped
}
The exception is raised once in a while. It is not raised every time I call the destructor, so I am not really sure what could be causing the segmentation violation.
This issue was moved by chriskohlhoff from boostorg/asio#143.