问题描述
调用向量的push_back方法时,将引发C ++异常.在调试器中,似乎在xmemory文件中引发了异常.这是发生异常的地方:
A C++ exception is being thrown when a vector's push_back method is called. In the debugger, it appears that the exception is being thrown deep in the xmemory file. Here is where I see the exception happen:
// TEMPLATE FUNCTION _Destroy
template<class _Ty> inline
void _Destroy(_Ty _FARQ *_Ptr)
{ // destroy object at _Ptr
_DESTRUCTOR(_Ty, _Ptr);
}
这似乎不是bad_alloc异常,因为我尝试使用bad_alloc catch处理程序将代码包装在try-catch中.该代码在那里执行了该步骤.它总是进入(...)捕获处理程序.如果不是bad_alloc异常,那可能是怎么回事?
It doesn't appear to be a bad_alloc exception because I tried wrapping the code in a try-catch with a bad_alloc catch handler. The code did that step in there. It always steps into the (...) catch handler. If it's not a bad_alloc exception, then what could be going on?
推荐答案
xmemory
标头是标准C ++库的Dinkumware实现的实现细节(例如,随MSVC ++一起提供).实际错误不太可能与该特定功能有关.我不知道宏 _DESTRUCTOR
扩展到什么(使用-E或/E编译器标志,您可以找到),但是它肯定会调用所涉及类型的析构函数.我将查看此宏引发的异常,或者检查我的析构函数是否可能引发异常.另外,为了更好地处理异常可能是什么,请尝试捕获 std :: exception const&
,因为建议抛出的每个异常都源自该类型.对于标准C ++库引发的所有异常,这绝对是正确的.但是,某些系统由于未定义的行为而引发异常,这些异常可能不是源自 std :: exception
(例如,当您的代码最终多次释放内存时).不幸的是,不遵循从 std :: exception
派生的建议,不幸的是调试异常要困难得多.当您捕获到 std :: exception const&
时,可以使用 std :: exception
的 what()
成员来查找其含义.做.
The xmemory
header is an implementation detail of the Dinkumware implementation of the standard C++ library (which is shipping e.g. with MSVC++). The actual error is very unlikely to be related to this particular function. I don't know what the macro _DESTRUCTOR
expands to (using the -E or /E compiler flag you can find out) but it will definitely call the destructor of the involved type. I would look at what exception this macro throws or check whether any of my destructors might throw an exception. Also, to get a better handle on what the exception might be, try to catch std::exception const&
as it is recommended that every exception being thrown derives from this type. This is definitely true for all exceptions thrown by the standard C++ library. However, some systems throw exceptions as a result of undefined behavior which may not derive from std::exception
(e.g. when your code ends up releasing memory multiple times). Not following this advice of deriving from std::exception
makes debugging exceptions unfortunately quite a bit harder. When you caught a std::exception const&
you can use the what()
member of std::exception
to find out what it is doing.
这篇关于C ++异常被深深地植入xmemory代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!