Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++: Call stack on assert failure

Is it possible to output the call stack when an assert fails in Release mode?

I can see the call stack inside Visual Studio when an assert fails in Debug mode. I have compiled Release mode with NDEBUG removed so that assertions are compiled. When this assert fails, the assert parameters are printed onto the console. It would be very helpful if I can somehow get an output of the call stack too.

like image 445
Ashwin Nanjappa Avatar asked Sep 05 '25 03:09

Ashwin Nanjappa


1 Answers

There are two functions that should help: CaptureStackBackTrace and StackWalk64 (requires Dbghelp.dll, here's an example).

Anyhow, both of them return you only a list of return addresses, so have to resolve them into function names manually. You can set VS to generate a MAP file with addresses of all functions in you executable. Here's how.

Also, don't forget that some functions may (and will) be inlined. So don't be surprised when instead of A -> B -> C you will see A -> C calls tack.

like image 161
yurymik Avatar answered Sep 07 '25 22:09

yurymik