C++ program crashes before theApp::InitInstance() - visual-c++-2005

Visual C++ 2005 Professional
Debug/Win32
Windows 7 Enterprise 64-bit
I set a breakpoint at my program's entrypoint. My program crashes before reaching that breakpoint. When the unhandled exception occurs (Access violation reading location 0x00000000.), I click on Break, and set a breakpoint where the current instruction pointer is located. Restart the debugging session and the program stops on the new breakpoint. I scroll up to the top of the current function, __tmainCRTStartup(), and set a breakpoint at the opening brace for the function.
I stop/restart the debugger again, this time stopping at __tmainCRTStartup(). By this time, all of the DLL's have been loaded, and if they needed to, run their load functions. Press [F5] to run to the original breakpoint. The Next Statement pointer is pointing at a call to WinMain(...).
Stepping over this statement causes the unhandled exception. Stepping into the statement takes me to this statement: "return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);". Stepping into once again takes me to the Disassembly window, where I see the next statement to run: "jmp AfxWinMain (563AFAh)".
I can step through the assembly code, but I cannot make out what it is trying to do before it tries to dereference a pointer in one of the registers, which holds a value of zero (0).
I read another post about a C program crashing before reaching main(). Even though tagged with "C", many responders mentioned constructors of a static instance of a class being a possible culprit. Others mentioned DLL's loading and running their own startup code that might crash the program, but the DLL's all seem to load and I have evidence of at least one DLL running its startup code, before the program gets to __tmainCRTSetup() .
Can anyone help me understand this? Knowing why it's doing this should help me understand how to fix it.
Thank you!

Related

Console Application exits on finish (Visual Studio)

It's not about code, though. But my console applications written in VS will exit as soon as the application finish its job (no matter debugging or directly opening by double clicking *.exe). How can I prevent this cause I need to see the output.
Actually it is about code. In your console application you have the main() function and because this is C++ it will end with a return 0; statement (or similar). To make your app pause when it has finished all you need to do is alter that last line to something like return fgetc(stdin); and it will now wait until you press the enter key.
But to expand on what BeyelerStudios said you might want to go a bit further than that and make it a bit smarter. So:
#ifdef DEBUG
_tprintf(L"Press the Enter key to exit\n");
return fgetc(stdin);
#else
return 0;
#endif
The Visual Studio Output window will display any string passed to the function OutputDebugString in DEBUG configuration. So if you just want a string you can look at, you can use that.
Passing arguments into you function is probably the best solution, you could still pause in release builds, but it is going to require the most work.
There is one final method. Rather than hit F5 open a command prompt (WindowsKey+R and type CMD), cd to the write directory and run you exe from there, all output will remain in the window after the program exists. But this won't help if you need the debugger.
Hope that helps.

Visual C++ Debugging Error - wuser32.pdb not loaded

I have made a form (GUI) in Visual C++ & while debugging it step by step, I got the following error, which stopped further debugging of the code.
The screenshot of the error is shown below.
How to solve this?
Good chance further debugging is in fact not stopped - seems you tried to step into a system function (implemented in wuser32.dll), and you can't debug this without matching symbols. You can post a screenshot of the stack window at this point to help us verify.
First, try to just continue (F5 or Shift+F11 to step out).
If for some reason you must view at least function names at this location - check the 'microsoft symbol server' at the screen you show. and click load. After a brief download pause, you should be able to see at least a disassembly window, and hopefully a meaningful name for the current stack frame.

Debugging Multithreaded C++ program in Visual Studio 2008

I am debugging a multi-threaded program in Visual Studio 2008.
When I break in the main thread, what is the status of the other threads in the process ?
They keep executing or they break as well ?
What happens to them when I do a F10/F11 in the main thread ?
How should I proceed if I need to check which thread has changed a particular variable ?
If the debugger breaks into the process (for example via Breakpoints or "Break All"), then all threads a get suspended.
If the press F10 or F11, then a new "temporary" breakpoint is added to the next line and the process is started (resumed) again. Therefore all threads runt (for a very short time), until the breakpoint is hit.
If you need to detect how is changing a variable, you can set a "Data-Breakpoint". For a reference see What are data breakpoints?
Also take a look at the documentation: How to: Set a Data Breakpoint
Also please be aware, that you can see the list of threads by opening the "Threads"-Window (Debug|Windows|Threads)!

intellij - not stopping on all breakpoints in multithreaded code

I am trying to run the following code with break points as follows:
new Thread(new Runnable() {
#Override
public void run() {
System.out.println("Starting"); //breakpoint here
}
}).start();
int i = 10;
i++; //breakpoint here
when this code runs ONLY the i++ breakpoint is hit... If I remove that one, the other thread's breakpoint would be hit correctly. Why is this weird behaviour occuring?
This is documented in http://www.jetbrains.com/idea/webhelp/breakpoints-2.html:
There are certain cases when IntelliJ IDEA will not stop at a breakpoint. Consider the following situation:
Two breakpoints are set at the different methods of a class, and there suspend policy is set to All.
When one of the breakpoints is hit, some step actions are performed.
If at the time of stepping another thread hits the second breakpoint, IntelliJ IDEA will not stop there.
I copied your code example and recreated the situation.
Sure enough, like it says in the documentation, after stopping at the i++ breakpoint, if I hit F8 (step over) the program doesn't stop on the other breakpoint. But if I hit F9 (resume) the program does stop again on the other breakpoint.
I just had this problem and for the sake of others that run into this, here is the reason for this behavior and how to change it.
As Doron pointed out, there is documentation concerning this. However, the IMPORTANT thing to notice is that by default, all threads in the JVM are suspended when a breakpoint is reached.
What you are expecting (and what I was expecting) is that only the thread with the breakpoint is suspended.
This isn't what you want, nor is it what I wanted.
To change this behavior (and provide the desired behavior).
1) Create a breakpoint by left clicking in margin.
2) Press ctrl+shift+F8 (to bring up the breakpoint menu).
3) Select your breakpoint. You will see options for it.
4) Make sure "Suspend" is checked and "Thread" radio option is selected.
5) Click the "Make default" button.
Now, when you run, you'll see that breakpoints in different threads are hit.
Because the other thread is scheduled to run in the background, and when the OS thread scheduler decides to run it, it will run. When you do have a breakpoint in it, it will be hit.
It won't have started necessarily when you just run through the code, so the breakpoint at i++ is hit immediately.

LINK : fatal error LNK1104: cannot open .exe

My question is not a duplicate of this, in fact its a extension of the same question,
This is the code snippet, due to which the error occurs,
BOOL CMyApp::InitInstance() {
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(SW_NORMAL);
m_pMainWnd->UpdateWindow();
return TRUE;
}
So, when I use m_pMainWnd->ShowWindow(SW_NORMAL); the prgoram runs without any error, the window opens in normal size and I can see the output, but when I do m_pMainWnd->ShowWindow(SW_MAX); even after the program runs without errors I cannot see the output window, also when I try to rebuild, VC throws an error saying
LINK : fatal error LNK1104: cannot open .exe
I have followed a few answers of this question as well, the first answer in this question suggests
You might have not closed the the output. Close the output, clean and rebuild the file. You might be able to run the file now.
which is quite correct but what I have to do is log off my computer and log in again in order to terminate the output process, I have to do this because I can neither find an application window that is open, nor I can see any program running in the Application's tab in 'Task Manager'. I even followed the second answer which says
You have to put Application Experience on Manual startup(you can do it by searching services in windows 7 start menu, and then find Application Experience and click properties).
except when I got there, I found the Application was already put on Manual Startup, and the problem still persists. Along with the solution what I want to know is why does the program not show output when I write m_pMainWnd->ShowWindow(SW_MAX);
If it helps I am using VC++ 6.0 and my OS is Windows-7 Professional 32-bit
SW_MAX is same as SW_FORCEMINIMIZE the documentation states that
Minimizes a window, even if the thread that owns the window is not
responding. This flag should only be used when minimizing windows from
a different thread.
It infact minimize the window and the application still run's in the task bar. I checked it in Win7 machine.
if your objective is to display the window in maximized state use SW_SHOWMAXIMIZED instead.
The VC++ 6.0 debugger does not work correctly under Windows 7 due to changes.
When you attempt to kill the process from the debugger, the process doesn't end correctly and gets stuck.
See the following topic for more info.
How to debug with Visual C++ 6 on Windows 7 x64?

Resources