Followers 0

# VC++ 2010 crashes on ShowWindow(..)

## 20 posts in this topic

I am trying to shift my project to my other computer. I think I have the compiler set up properly. Initially, I thought it was DX causing the issue, but I narrowed it down to WinMain(..). Here is the code:

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
// wc.hbrBackground = (HBRUSH)COLOR_WINDOW;    // not needed any more
wc.lpszClassName = "WindowClass";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
"WindowClass",
"Our Direct3D Program",
WS_EX_TOPMOST | WS_POPUP,    // fullscreen values
0, 0,    // the starting x and y positions should be 0
int(SCREEN_WIDTH),int( SCREEN_HEIGHT),    // set the window size
NULL,
NULL,
hInstance,
NULL);
//return 0;//exits without crashing
ShowWindow(hWnd, nCmdShow);
return 0;//it will crash here
// set up and initialize Direct3D
GameEngine.initD3D(hWnd,hInstance);

//init the environment here.

// enter the main loop:
UINT ExitMessage=GameEngine.MainLoop();

return ExitMessage;
}



Notice the 2 return lines arround ShowWindow(..)? That's how I came to the conclusion it had something to do with showing the window. I believe it's the creation that's actually causing the problem, but I don't know what could cause it to work on one machine and not another. Perhapse I don't have the correct SDK (VC) or I don't have my compiler set up correctly????

0

##### Share on other sites

Im pretty sure it crash inside GameEngine.initD3D(hWnd,hInstance), but without further information we can't help you much. Try debuggging it and see where it really crash. ShowWindow returning 0 is perfectly normal. Is hWnd NULL or not?

Restored post contents from history.
1

##### Share on other sites

You're not checking the return value from your CreateWindowEx call (a run in your debugger with a breakpoint set at your ShowWindow call would have told you that you didn't have a valid HWND here).  Also, some of your CreateWindowEx parameters are wrong - please review the documentation at http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680%28v=vs.85%29.aspx and correct them before continuing any further with this.

If it worked on one PC it was certainly by accident rather than by design.

I am trying to shift my project to my other computer. I think I have the compiler set up properly. Initially, I thought it was DX causing the issue, but I narrowed it down to WinMain(..). Here is the code:

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
// wc.hbrBackground = (HBRUSH)COLOR_WINDOW;    // not needed any more
wc.lpszClassName = "WindowClass";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
"WindowClass",
"Our Direct3D Program",
WS_EX_TOPMOST | WS_POPUP,    // fullscreen values
0, 0,    // the starting x and y positions should be 0
int(SCREEN_WIDTH),int( SCREEN_HEIGHT),    // set the window size
NULL,
NULL,
hInstance,
NULL);
//return 0;//exits without crashing
ShowWindow(hWnd, nCmdShow);
return 0;//it will crash here
// set up and initialize Direct3D
GameEngine.initD3D(hWnd,hInstance);

//init the environment here.

// enter the main loop:
UINT ExitMessage=GameEngine.MainLoop();

return ExitMessage;
}



Notice the 2 return lines arround ShowWindow(..)? That's how I came to the conclusion it had something to do with showing the window. I believe it's the creation that's actually causing the problem, but I don't know what could cause it to work on one machine and not another. Perhapse I don't have the correct SDK (VC) or I don't have my compiler set up correctly????

If I am not wrong...WS_EX_TOPMOST is an extended style, the extended styles shoud be in the first parameter and the "normal" styles in the fourth

So it should be CreateWindowEx(WS_EX_TOPMOST,  "WindowClass", "Our Direct3D Program", WS_POPUP,...

Probably the WS_EX_TOPMOST is being interpreted wrong and there is a conflict with the WS_POPUP

Edited by Ravnock
1

##### Share on other sites

I think it was a problem with my WNDCLASSEX. I adjusted the parameters and now it doesn't crash-- it just exits. I checked hWnd and it's NULL. Shouldn't it have a value? Here is the updated code:

 int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;

WNDCLASSEX windowClass;
windowClass.lpszClassName = "Main Class";
windowClass.cbClsExtra = NULL;
windowClass.cbWndExtra = NULL;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(150,0,0));
windowClass.hInstance = hInstance;
windowClass.lpfnWndProc = WindowProc;
windowClass.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&windowClass);

hWnd = CreateWindowEx(WS_EX_TOPMOST,//WS_EX_TOPMOST
"WindowClass",
"Our Direct3D Program",
WS_POPUP,    // fullscreen values
0, 0,    // the starting x and y positions should be 0
int(SCREEN_WIDTH),int( SCREEN_HEIGHT),    // set the window size
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);// nCmdShow
// set up and initialize Direct3D
GameEngine.initD3D(hWnd,hInstance);

//init the environment here.
// enter the main loop:
UINT ExitMessage=GameEngine.MainLoop();

return ExitMessage;
}



What could I be doing wrong? The exit occurs just after I d3d->CreateDevice(..) because I don't get a pointer to the device.

0

##### Share on other sites

Thanks for the advice guys/gals. Now it seems to get everything up to ShowWindow(..). I get a red screen and it crashes:

 int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;

WNDCLASSEX windowClass;
ZeroMemory(&windowClass,sizeof(WNDCLASSEX));
windowClass.lpszClassName = "WindowClass";
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(150,0,0));
windowClass.hInstance = hInstance;
windowClass.lpfnWndProc = WindowProc;
windowClass.style = CS_VREDRAW | CS_HREDRAW;
//    RegisterClassEx(&windowClass);
if(!RegisterClassEx (&windowClass))
{
MessageBox (NULL, "Class registration failed!", "Error!", MB_OK);
PostQuitMessage (0);
return 1;
}

hWnd = CreateWindowEx(WS_EX_TOPMOST,//WS_EX_TOPMOST
"WindowClass",
"Our Direct3D Program",
WS_POPUP,    // fullscreen values
0, 0,    // the starting x and y positions should be 0
int(SCREEN_WIDTH),int( SCREEN_HEIGHT),    // set the window size
NULL,
NULL,
hInstance,
NULL);

if(!hWnd)
{
MessageBox (NULL, "hWnd NULL", "Error!", MB_OK);
PostQuitMessage (0);
return 1;
}
ShowWindow(hWnd, nCmdShow);// nCmdShow
// set up and initialize Direct3D

return 0;//just want to exit the program to rule out DirectX......

GameEngine.initD3D(hWnd,hInstance);

//init the environment here.
// enter the main loop:
UINT ExitMessage=GameEngine.MainLoop();

return ExitMessage;
}



As you can see, I exit just after ShowWindow(..), so there's nothing else running in the program. WHAT AM I DOING WRONG!!! This is frustrating. The original post's code worked just fine on my other computer, but somehow it doesn't work on this one.....????

0

##### Share on other sites

WHAT AM I DOING WRONG!!!!

You're not running in your debugger.

Make a debug build, run it under your debugger, and it will tell you what line of code it crashed on, let you inspect the values of variables (to help you find out what crashed it) and lots of other good stuff.

1

##### Share on other sites

I put a break at ShowWindow(..). Here is the output (image):

That is the farthest I can put a break without it crashing. When it crashes, I don't get anything but a red screen (that's the color I have for the background) and when I look at the task manager, it says the program is not responding.....

I was able to get a snapshot of an error. It is in "crt0dat.c". I'm thinking I have issues with my compiler.....

0

##### Share on other sites
While I do not know the default value for nCmdShow try displaying your window using SW_SHOW (Also seen here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx).

If nCmdShow is uninitialized, which it could or could not be honestly I have never checked, you would get an error with your program. If that doesn't help - sorry :P
2

##### Share on other sites

According to MS, that parameter should be taken from WinMain parameter. I tried it anyway. Same issue.

0

##### Share on other sites

Actually make a new project and paste your code there and then post the entire code if it still crashes, don't include stuff that isn't used. Perhaps you use something from WndProc that isn't created yet. Try putting ShowWindow right before the main-loop after D3D is set up etc.

1

##### Share on other sites

That's what I did. Just finished cleaning it up so it would work. It runs but that doesn't explain what caused the issue in the first place.....

0

##### Share on other sites

Just guessing unless you post the whole code in its entirety, but when you call ShowWindow it will call your WindowProc with a couple of messages, and perhaps the D3D device was used there, before it was initialized, or similar.

2

##### Share on other sites

I don't have the code that isn't working. I moved all my files into another project and I got it to work. That's actually funny: the "solution" was the "problem"....

0

##### Share on other sites
Access violation reading location 0x00000000


You've a NULL pointer somewhere that you're trying to use.  Again, this is something that your debugger will tell you.  At this stage you're getting into fairly basic stuff and essentially asking us to debug your code for you.  I don't mean to be rude here, but you're really not showing many signs of trying very hard - or at all - yourself.  If you had run this in your debugger it would have halted execution at the offending line, enabling you to determine exactly what has happened.  If you had been using your debugger you would have stepped through line-by-line and found what you were doing wrong.  Which leads me to suspect that you haven't.

Edited by mhagain
1

##### Share on other sites

I did run the debugger. The "offending line" had something to do with the exit of the program. That didn't tell me anything. The problem was some kind of corruption in my VC++ solution file. When I started over with a new solution, the problem went away. Debug will only show me the answer if the problem is in my program. This went beyond that, so to say I wasn't trying is a bit insulting.

0

##### Share on other sites

A general advice from my experience as a programmer: every time I think the compiler/the framework is wrong, it always turns out *I* did something wrong.

In C++ Win32/COM programming, you have to understand the error model, which is quite different from the easy-peasy world of .NET/Java. As others said before, CreateWindow/CreateWindowEx can fail for many reasons - most probably because of incorrect arguments or WinProc. In the first case, GetLastError() and Google would help you, and in the case of WinProc - a few well-placed breakpoints at key messages would pin-point the problem. You should also absolutely always check return codes... this isn't Java/.NET where something crashes with a nice exception - return codes and/or GetLastError() are the way.

1

##### Share on other sites

I get your point. The thing is, I took my original code files and put them into a new project and it worked fine. The problem started when I transferred my project from one computer to another, something happened..... Maybe there was something wrong with the project/solution or I had some update issues with the new computer/compiler that caused it. So ultimately this problem WAS something other than my program.

0

##### Share on other sites

I don't have the code that isn't working. I moved all my files into another project and I got it to work. That's actually funny: the "solution" was the "problem"....

That sounds like you should learn about using a version control system to not loose code. Mercurial(hg) would be easiest to learn, but git is also widely used. (Even if you dont want to learn one of those at least start with the poor mans "version control" of copying the whole folder every time before you change anything and number those by date.)

0

## Create an account

Register a new account