C++ and dump file

learningunix
learningunix used Ask the Experts™
on
I am working on application that creates crash and it is difficult to debug the memory crash.

I am using Visual studio 2005

Is there a sample C++ code that will create crash on windows.
1. How to create memory/mini dump .dmp file for this c++ application
2. How to open this .dmp file once it is created so that I can start debugging to find where it is failing (function variable etc...)
3. I have the location to .pdb files.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Nothing special. The attached program crashes on Windows XP. In the second loop. The memory was allocated for one integer but the program writes 12 integers (in the first loop).

If you want us to help you need to provide a bit more info.
You are debugging a C++ application in Visual Studio and you see a crash. What is this a message box informing about an error with three buttons: Break, Continue and Retry?

If I'm right, please clean the solution and re-build it. Then reproduce the crash. Post here the screenshot with the error message box. The debugger stops somewhere in your code. Analyze this code.

In 80%, this is something about a pointer. For example you call a method like here:
pMyObject->Amethod();
and pMyObject is 0.

#include <iostream>

int main (int argc, char * const argv[]) 
{
        int * ivar = new int(12);
        for (int i = 0; i < 12; ++i)
                ivar[i] = i;
        
        for (int k = 0; k < 12; ++k)
                std::cout << "[" << k << "] = " << ivar[k] << "\n";

        delete [] ivar;
    return 0;
}

Open in new window

Author

Commented:
Thanks for your reply. I know how to create a crash.

One example is:  
char *tmp = (char *) malloc (5);
strcpy(tmp, "123456");
free (tmp);

My question is if the have a .dmp file.

1) I can open the .dmp file from VS 2005, but how can I debug the file. i need to see call stack and variable values etc...
2) what settings do i need? (I know the location of .pdb file)

I have 2008 Express so I cannot help you directly. The following links may help get you started with debug tools (windbg, etc.) but pgnatyuk likely can help you through the details when he gets back here.

How to Analyse a Dump File:
http://clintboessen.blogspot.com/2009/12/how-to-analyze-dump-file.html

How to generate a dump when your 2005 app crashes:
http://www.developmentnow.com/g/51_2007_2_0_0_933291/Generating-always-a-dmp-file-when-my-app-crashes-on-WinXP.htm

Author

Commented:
I tried using Windbg, I get  
*** ERROR: Symbol file could not be found

I am sure i m missing something here... I did point symbol file path to the location where all .pdb files are located.
Commented:
I do not understand why you need WinDbg. Do you work with a release build? Do you have a trace command in the code and you see them in them WinDbg?

Any expert will tell you that in Visual Studio you have one of the best debuggers. Are you talking about Visual Studio 2005 Professional? From my point, now it's one of the best and stable versions released by Microsoft.

You asked about the code that produces the crash:
"Is there a sample C++ code that will create crash on windows."

If you have the source code of this program, re-build the debug configuration of this solution (I insist on it) and debug it in Visual Studio. Can you reproduce the crash here in this environment?
I'd say, sometimes it is enough simply to re-build the solution.
If you can reproduce the crash, Visual Studio shows you an error message box. What is written in this message box. All technical information (including the memory dump) is available from this point. Pause the debugging and use VS Debug menu to investigate what's going on - you can see the stack and find the code caused the crash. It can be in different thread, and you can see all threads. You can check the variables and the memory.

You want a dump of what? Of 2GB of the virtual memory on your computer?
Probably, it will not make any sense. When you see the error in the Visual Studio, tell us what is written there and we will tell you how to handle this situation.

Commented:
>>*** ERROR: Symbol file could not be found
WinDbg does not find an object. Probably a function in a system dll (ntdll.dll?). WinDbg lazily loads the symbols. That's all.
Resolving Symbol Problems in WinDBG
http://www.osronline.com/article.cfm?id=221

BTW, it can be a correct information - a function cannot be found in a dll you load dynamically, for a example. Do you have something with GetProceAddress? If yes, check if the function pointer is not NULL before you call it.

If you want to get complicated with the debuggers:
How to solve Windows system crashes in minutes
http://www.networkworld.com/news/2005/041105-windows-crash.html?page=2



hey pgnatyuk,

I like your suggestions. And the VS debugger for us was excellent when programs crashed. We even integrated Parasoft's Insure++ into VC++ 6.0 that found many different kinds of real-time errors even when the program did not crash. In fact we often built programs in VS to use its great debugger, and then port it to the target platform.

What to do if the crash is rare and occurs on a target platform (as often is the case); and a crash in the debugger is not easy to do? Then, if all you have is a crash dump, is it possible to somehow analyze it using MS debugging tools to figure out what's wrong?

===
As an aside..

In a different environment (SCO Unix) when we had a lab platform crash that did not occur in the development system, we used Insure++ (for SCO) on different modules to assess the run-time errors.

Maybe Insure++ for VS C++ could be of use to catch the run-time problems (whether it causes a crash or not) in both the debugger and on the target platform. (Caveat - it does slow down processing, so if the app is a real-time intensive program, then you may have to apply Insure++ one module at a time.)

Commented:
Mainly, on Windows I work with BoundChecker and Intel Purify+.
I heard about Insure.
Infinity08 in one of my threads recommended ValGrind:
http://www.experts-exchange.com/Programming/Languages/CPP/Q_26302622.html
http://valgrind.org/

You know that a Trace and Log tools works always. :)
That's what I used when had to work with Windows Mobile/CE devices.

For Mac OS, Xcode has many tools, including the static analysis tools. But it's all works great with Objective-C and ... from my point of view, it is bad with C++. :(

Actually, on a Windows computer BoundChecker (the latest versions wish to be better, but that's what we have) always allowed me to fix the problem. But we use it only if we cannot detect the problem with the standard debugger. You know sometimes it's enough to switch on exceptions in the debug menu.

I do not program for Unix. Ask evilrix about it.


>> tried using Windbg, I get  
*** ERROR: Symbol file could not be found

Not a major problem if ur symbol file(.pdb) is located perfectly.
In Windbg-> File->Symbol file path, u need to setup ur .pdb's path.
u also can use symbol server SVR*. for more info see the documentation.

Launch ur app , after getting crash, goto File->Attach to process, Locate ur exeucutable.

U can use !analyze -V command and the see the stack, if u setup te source file's path, then u can also reach at the line where crash is occuring.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial