boost::asio 暂停时抛出异常
boost::asio throws exception when paused
我有一个程序,它使用boost::asio
连接到远程机器,然后反复打印出它收到的任何内容。问题是,每当我在运行时暂停它或对断点进行任何更改时,都会从 read_until()
内部的某个地方引发异常。为什么会发生这种情况,我该怎么办?
这是在运行OS X 10.8.2的Mac上,Xcode 4.4.1和Apple clang 4.0。暂停程序后引发异常时的堆栈跟踪:
* thread #1: tid = 0x1d07, 0x00007fff86bc9d46 libsystem_kernel.dylib`__kill + 10, stop reason = signal SIGABRT
frame #0: 0x00007fff86bc9d46 libsystem_kernel.dylib`__kill + 10
frame #1: 0x00007fff8ec40df0 libsystem_c.dylib`abort + 177
frame #2: 0x00007fff8c49ca17 libc++abi.dylib`abort_message + 257
frame #3: 0x00007fff8c49a3c6 libc++abi.dylib`default_terminate() + 28
frame #4: 0x00007fff8d05e887 libobjc.A.dylib`_objc_terminate() + 111
frame #5: 0x00007fff8c49a3f5 libc++abi.dylib`safe_handler_caller(void (*)()) + 8
frame #6: 0x00007fff8c49a450 libc++abi.dylib`std::terminate() + 16
frame #7: 0x00007fff8c49b5b7 libc++abi.dylib`__cxa_throw + 111
frame #8: 0x00000001000043df test`void boost::throw_exception<boost::system::system_error>(boost::system::system_error const&) + 111 at throw_exception.hpp:66
frame #9: 0x0000000100004304 test`boost::asio::detail::do_throw_error(boost::system::error_code const&, char const*) + 68 at throw_error.ipp:38
frame #10: 0x0000000100004272 test`boost::asio::detail::throw_error(boost::system::error_code const&, char const*) + 50 at throw_error.hpp:42
frame #11: 0x0000000100002479 test`unsigned long boost::asio::read_until<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >, std::allocator<char> >(boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&, boost::asio::basic_streambuf<std::allocator<char> >&, std::string const&) + 73 at read_until.hpp:98
frame #12: 0x00000001000012c5 test`main + 581 at main.cpp:21
frame #13: 0x00007fff8983e7e1 libdyld.dylib`start + 1
当您暂停程序时,实际暂停是通过向其发送 POSIX 信号 ( SIGSTOP
) 来完成的。这样做的影响之一是系统调用(例如 read()
,Boost 将在内部使用)返回错误,EINTR
。这将触发read_until
的错误处理代码,如您所见,该代码会引发异常。
如果要正确处理此问题,则可能需要使用采用boost::system::error_code
参数的重载,根据EINTR
(在errno.h
中定义)检查.value()
,然后重试读取。
这看起来像
boost::system::error_code error;
boost::asio::streambuf message;
do {
boost::asio::read(socket, message, boost::asio::transfer_exactly(body_size), error);
} while (error.value() == EINTR);
read_until()
有一个覆盖,会在错误时引发异常,如果你没有抓住这个,那么你会看到这个行为。如果您使用的是不接收boost::system::error_code&
的boost::asio
覆盖,那么为了安全起见,您应该将这些调用包装在捕获const boost::system::error_code&
的try
块中。 在异常处理程序中,应检查异常以查看失败的根本原因。
try
{
boost::asio::read_until(...);
}
catch(const boost::system::error_code& err)
{
// read_until(...) failed, the reason is
// contained in err
}
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- 函数如何通知用户它基于函数原型抛出异常?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- VisualStudios 会抛出异常,而代码块不会 [C++]
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- ZMQ::send() 抛出异常并终止 QNX 进程.为什么以及如何从中恢复?
- new(std::nothrow) int[n] 抛出异常
- 为什么g_object_set抛出异常(vcruntime140.dll)?
- 你能防止 std::regex 在无效表达式上抛出异常吗?
- C++子线程抛出异常
- 我们应该在抛出异常之前取消分配内存吗
- 有没有一种方法可以让OpenCLC++绑定为所有错误抛出异常
- 为什么访问模板化变体作为返回值抛出异常
- 如何实现,错误分配中止而不是抛出异常
- std::p riority_queue::p op什么时候会抛出异常
- 在0x5914F3BE抛出异常(基于.dll)
- std::vector 默认构造函数可以抛出异常吗?
- 挂钩创建文件抛出异常:读取访问冲突
- boost::asio 暂停时抛出异常
- Boost.Asio应用程序在创建接收器对象时抛出异常