views:

1836

answers:

7

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)?

+5  A: 

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.

Brian R. Bondy
Well, at least you can see which library call caused the crash.
Thomas
In the intermediate window of VS you can type (if you have WinDBG): ".load sos.dll" (without the quotes) and use the SOS debugger as well.
sixlettervariables
A: 

If I remember correctly that message box should have a button which says 'retry'. This should then break the program (in the debugger) at the point where the assertion happened.

roo
+3  A: 

If you run the debug version on a machine with VS, it should offer to bring it up and let you see the stack trace.

The problem is that the real problem is not on the call stack any more. If you free a pointer twice, that can result in this problem somewhere else unrelated to the program (the next time anything accesses the heap datastructures)

I wrote this blog on some tips for getting the problem to show up in the call stack so you can figure out what is going on.

http://www.atalasoft.com/cs/blogs/loufranco/archive/2007/02/06/6-_2200_Pointers_2200_-on-Debugging-Unmanaged-Code.aspx

The best tip is to use the gflags utility to make pointer issues cause immediate problems.

Lou Franco
Thanks for the info on gflags I never knew about it.
Brian R. Bondy
I have a feeling this utility will save me months of work in the future :)
Brian R. Bondy
Definitely -- as soon as I get any kind of memory crash, I just use the Page Heap feature -- saves a lot of time.
Lou Franco
A: 

CrashFinder can help you locate the place of the exception given the DLL and the address of the exception reported.
You can take this code and integrate it into your application to have a stack trage automatically generated when there is an uncaught exception. This is generally performed using __try{} __except{} or with a call to SetUnhandledExceptionFilter which allows you to specify a callback to all unhandled exceptions.

shoosh
+1  A: 

You can trigger a mini-dump by setting a handler for uncaught exceptions. Here's an article that explains all about minidumps

Google actually implemented their own open source crash handler called BreakPad, which also mozilla use I think (that's if you want something more serious - a rich and robust crash handler).

Assaf Lavie
+1  A: 

This article describes how to compute a stack trace.

Xavier Nodet
A: 

You can also have a post-mortem debugger installed on the client system. This is a decent, general way to get information when you do not have dump creation built into your application (maybe for an older version for which you must still get information).

Dr. Watson on Windows can be installed by running: drwtsn32 -i Running drwtsn32 (without any options) will bring up the configuration dialog. This will allow the creation of crash dump files, which you can later analyze with WinDbg or something similar.

Kris Kumler