• Create Account

VC++ 2010 crashes on ShowWindow(..)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

20 replies to this topic

#1Hawkblood  Members

954
Like
0Likes
Like

Posted 16 March 2013 - 11:47 AM

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

#2Vortez  Members

2705
Like
1Likes
Like

Posted 16 March 2013 - 11:56 AM

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?

Edited by jbadams, 04 April 2013 - 05:46 AM.
Restored post contents from history.

#3mhagain  Members

12438
Like
4Likes
Like

Posted 16 March 2013 - 12:14 PM

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.

Edited by mhagain, 16 March 2013 - 12:14 PM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#4Ravnock  Members

307
Like
1Likes
Like

Posted 16 March 2013 - 01:42 PM

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, 16 March 2013 - 06:41 PM.

#5Hawkblood  Members

954
Like
0Likes
Like

Posted 16 March 2013 - 07:51 PM

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.

#6Endurion  Members

5168
Like
3Likes
Like

Posted 16 March 2013 - 11:46 PM

Your registered class and the class passed in CreateWindowEx do not match ("Main Class" vs. "WindowClass")

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#7mhagain  Members

12438
Like
3Likes
Like

Posted 17 March 2013 - 04:46 AM

And you're still not checking your return values - from neither RegisterClassEx nor CreateWindowEx.

I can't emphasise this enough - don't just plough on ahead as if everything succeeded - check your return values, they're there to help you.  In this case you'd see that your RegisterClassEx call actually worked, which would have told you that your problem was either in the CreateWindowEx call or the connection between the two (i.e. the class name).

Also, please don't use "NULL" for struct members defined as "int".  Yes, "#define NULL 0" allows you to, but it looks wrong, and working code should not look wrong.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#8Hawkblood  Members

954
Like
0Likes
Like

Posted 17 March 2013 - 06:51 AM

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

#9mhagain  Members

12438
Like
1Likes
Like

Posted 17 March 2013 - 07:37 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#10Hawkblood  Members

954
Like
0Likes
Like

Posted 17 March 2013 - 08:23 AM

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.....

#11AnnaMarie  Members

2032
Like
2Likes
Like

Posted 17 March 2013 - 08:55 AM

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

#12Hawkblood  Members

954
Like
0Likes
Like

Posted 17 March 2013 - 09:06 AM

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

#13Erik Rufelt  Members

5901
Like
1Likes
Like

Posted 17 March 2013 - 02:03 PM

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.

#14Hawkblood  Members

954
Like
0Likes
Like

Posted 17 March 2013 - 06:43 PM

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.....

#15Erik Rufelt  Members

5901
Like
2Likes
Like

Posted 17 March 2013 - 07:43 PM

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.

#16Hawkblood  Members

954
Like
0Likes
Like

Posted 17 March 2013 - 08:50 PM

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"....

#17mhagain  Members

12438
Like
1Likes
Like

Posted 17 March 2013 - 08:50 PM

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, 17 March 2013 - 08:54 PM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#18Hawkblood  Members

954
Like
0Likes
Like

Posted 17 March 2013 - 08:57 PM

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.

#19dilyan_rusev  Members

1360
Like
1Likes
Like

Posted 19 March 2013 - 12:29 AM

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.

#20Hawkblood  Members

954
Like
0Likes
Like

Posted 19 March 2013 - 03:47 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.