Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a stack trace when C++ program crashes? (using msvc8/2005)

Sometimes my c++ program crashes in debug mode, and what I got is a message box saying that an assertion failed in some of the internal memory management routines (accessing unallocated memory etc.). But I don't know where that was called from, because I didn't get any stack trace. How do I get a stack trace or at least see where it fails in my code (instead of library/ built-in routines)?

like image 866
Randy Sugianto 'Yuku' Avatar asked Sep 22 '08 02:09

Randy Sugianto 'Yuku'


People also ask

What is crash stack trace?

Crashes and ANRs on Android produce a stack trace, which is a snapshot of the sequence of nested functions called in your program up to the moment it crashed. These snapshots can help you identify and fix any problems in the source.

How do I get full stack trace in Visual Studio?

To open the Call Stack window in Visual Studio, from the Debug menu, choose Windows>Call Stack. To set the local context to a particular row in the stack trace display, select and hold (or double click) the first column of the row.


1 Answers

If you have a crash, you can get information about where the crash happened whether you have a debug or a release build. And you can see the call stack even if you are on a computer that does not have the source code.

To do this you need to use the PDB file that was built with your EXE. Put the PDB file inside the same directory as the EXE that crashed. Note: Even if you have the same source code, building twice and using the first EXE and the second PDB won't work. You need to use the exact PDB that was built with your EXE.

Then attach a debugger to the process that crashed. Example: windbg or VS.

Then simply checkout your call stack, while also having your threads window open. You will have to select the thread that crashed and check on the callstack for that thread. Each thread has a different call stack.

If you already have your VS debugger attached, it will automatically go to the source code that is causing the crash for you.

If the crash is happening inside a library you are using that you don't have the PDB for. There is nothing you can do.

like image 198
Brian R. Bondy Avatar answered Oct 13 '22 14:10

Brian R. Bondy