简体   繁体   中英

C++ exception being thrown deep in xmemory code

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);
}

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?

The xmemory header is an implementation detail of the Dinkumware implementation of the standard C++ library (which is shipping eg 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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM