VC++ 2010 crashes on ShowWindow(..)

Started by
19 comments, last by wintertime 11 years, 1 month ago

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.hCursor = LoadCursor(NULL, IDC_ARROW);
    // 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????

Advertisement

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?

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.hCursor = LoadCursor(NULL, IDC_ARROW);
    // 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

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.hCursor = LoadCursor(NULL,IDC_ARROW);
    windowClass.hIcon = LoadIcon(NULL,MAKEINTRESOURCE(IDI_APPLICATION));
    windowClass.hIconSm = (HICON) LoadImage(NULL,MAKEINTRESOURCE(IDI_APPLICATION),IMAGE_ICON,16,16,NULL);
    windowClass.hInstance = hInstance;
    windowClass.lpfnWndProc = WindowProc;
    windowClass.lpszMenuName = NULL;
    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.

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>

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.hCursor = LoadCursor(NULL,IDC_ARROW);
    windowClass.hIcon = LoadIcon(NULL,MAKEINTRESOURCE(IDI_APPLICATION));
    windowClass.hIconSm = (HICON) LoadImage(NULL,MAKEINTRESOURCE(IDI_APPLICATION),IMAGE_ICON,16,16,NULL);
    windowClass.hInstance = hInstance;
    windowClass.lpfnWndProc = WindowProc;
    windowClass.lpszMenuName = NULL;
    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.....????

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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

This topic is closed to new replies.

Advertisement