My C++ program exits with a std::logic_error and I’d like to track down the source line that caused it. How can I do that?
TBH, I’m using gdb, using g++ -g in order to add debug info. All I can get are these messages:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application’s support team for more information.
terminate called after throwing an instance of ‘std::logic_error’
what(): basic_string::_S_construct null not valid
Catchpoint 1 (exception thrown), 0x0045ffa0 in __cxa_throw ()
(gdb) bt
#0 0x0045ffa0 in __cxa_throw ()
#1 0x004601e8 in std::__throw_logic_error(char const*) ()
#2 0x00502238 in typeinfo for std::__timepunct<wchar_t> ()
#3 0x004685f8 in std::runtime_error::what() const ()
#4 0x03210da8 in ?? ()
#5 0x002efbcc in ?? ()
#6 0x00468734 in std::domain_error::~domain_error() ()
#7 0x00000000 in ?? ()
(gdb)
The exception objects don’t carry any source information with them. However, they hopefully contain a useful message accessible using the
what()member. Other than that you’d either have to use a debugger allowing to break when exceptions are thrown or set a break point into the constructor ofstd::logic_error. As long as exceptions are exceptional this works OK. It doesn’t work too well with code throwing exceptions in non-exceptional cases.