Thread: ShowWindow causing Access Violations - please help

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    22

    Angry ShowWindow causing Access Violations - please help

    I'm trying to write a WIN32 API C++ wrapper (and I'm new to WIN32 programming but I think I understand the concepts fairly well), and I'm hitting a problem with the ShowWindow(HWND hWnd, int nShowCmd) function. I've read several articles and tutorials on the API and making wrapper classes for it, but I've seen no mention of this. The window structure is being properly filled out, the window is being registered and it is being created. But when I call ShowWindow, it craps out - the window appears (slowly) but is busy and not responding. According to the MSVC++ 6.0 debugger, an access violation is being cause when ShowWindow is called. Any help would be greatly appreciated, as this problem is a major show stopper.

    Below is the code for my show window method (at the very bottom) and the window creation method. The window handle, hWnd, is a protected member of the class. AddErrorMessage is an inherited method for posting error messages to a queue so they can be written to a file later. The class is modeled off of several examples I have.



    bool WindowClass::WindowCreate(HINSTANCE hInstance, char *title, int x, int y, int width, int height, unsigned long classStyle, unsigned long windowStyle)
    {
    WNDCLASS window;

    /* Configure Window Structure */
    window.style = classStyle;
    window.lpfnWndProc = WindowProc;
    window.cbClsExtra = 0;
    window.cbWndExtra = 0;
    window.hInstance = hInstance;
    window.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    window.hCursor = LoadCursor (NULL, IDC_ARROW);
    window.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    window.lpszMenuName = NULL;
    window.lpszClassName = title;

    /* Register Window*/
    if(!RegisterClass(&window))
    {
    int error = GetLastError();
    AddErrorMessage("Unable to register window", "WindowCreate", "WindowClass", __FILE__, __LINE__);
    return false;
    }

    /* Create Window and Assign Handle */
    hWnd = CreateWindow(title, title, windowStyle, x, y, width, height, NULL, NULL, hInstance, (void*)this);
    if(hWnd != NULL)
    {
    windowStatus = WINDOW_ALIVE;
    return true;
    }
    else
    {
    windowStatus = WINDOW_NOT_CREATED;
    AddErrorMessage("Unable to create window", "WindowCreate", "WindowClass", __FILE__, __LINE__);
    return false;
    }

    }


    void WindowClass::WindowShow(int nShowCmd)
    {
    if(hWnd != NULL)
    {
    ShowWindow(hWnd, nShowCmd); // THIS IS THE PROBLEM!
    UpdateWindow(hWnd);
    }
    }

    Thanks!
    Last edited by Arrow Mk 84; 08-03-2002 at 08:11 PM.
    Never give an Ewok a thermial detonator

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I can't tell with what you've posted. It may be a problem in your window procedure that only manifests itself when you attempt to display the window - perhaps look to initialisation code in any WM_CREATE,WM_NCCREATE,WM_NCCALCSIZE ie any of the messages sent during Windows processing of CreateWindow for possible problems.

    Failing that, you could always zip and post the project...

    Good luck anyway.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    22
    I looked at the WNDPROC function as you suggested (and rewrote half of my code so that the WNDPROC is a global C function instead of a static function in a C++ wrapper). Their seems to be a problem with hWnd when I try to create the window. Here's the flow:

    WinMain creates an instance of the window class and then calls it create method, feeding it the hInstance, the WndProc function and the parameters for the window size and style. The create method then fills out the WNDCLASS structure and registers it. Then it calls CreateWindow to get the HWND for the window. At this point, the WNDPROC is entered and it tries to retrieve the information for the window (message code is 36), at which point it gets an access violation (x00..0c5 I believe).

    Attached is a zip file of the code. It is going to be base of a 3D engine (using openscenegraph) IF I can ever get it to work! Most of the finish code is documented (but it may not be up to date after this morning). Also, my compiler is set to grab include files from a directory specific to my project, so all of my #includes are <>.

    This is my first real WIN32 program (most of my stuff has been console apps, libraries tested on console apps and Java).

    Thanks and I hope some one can tell me just whats going wrong.
    Ryan
    Never give an Ewok a thermial detonator

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    During the call to CreateWindow or CreateWindowEx a whole bunch of msgs are sent as I mentioned previously. In your wndproc, regardless of the static or global nature of the fn, you are attempting to cast a whole bunch of crap into your class wrapper ptr when msg=36 (WM_GETMINMAXINFO).

    This is a consequence of using this particular technique for message handling: you lose the ability to handle this msg (which is not really a problem).

    Anyway in your window procedure, replace:
    Code:
    if (msg == WM_NCCREATE || msg == WM_CREATE || msg == 36)
    with:
    Code:
    if (msg == WM_NCCREATE || msg == WM_CREATE)
    and that should work fine.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    22
    Thanks for your reply. I made the change you suggested, and the window now appears (slowly, as if it stalls). When I place the mouse over the window, the I get the hour-glass cursor. Ctrl-alt-delete says that the window is not responding.

    Did you by chance compile the code on your machine? Maybe the problem lies somewhere else.

    Also, what do you think is the best way to code Win32? Straight C or using C++ wrappers?

    Thanks again.
    Never give an Ewok a thermial detonator

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>and the window now appears (slowly, as if it stalls)<<

    OpenGL initialisation does take a little time, yes. You could try delaying showing the window until after all your initialisation is complete.

    >>When I place the mouse over the window, the I get the hour-glass cursor<<

    I suspect that the message filter thingy you are using (MessageLookupTable?) is not permitting default processing of the WM_SETCURSOR which would ensure that your wnd class (as in the registered 'class' as opposed to c++ class) cursor is properly displayed. I think it may not be coping with WM_DESTROY/WM_CLOSE and possibly other messages too.

    >>Also, what do you think is the best way to code Win32? Straight C or using C++ wrappers?<<

    I don't feel qualified to comment on 'best'. Personally, I prefer to use c++ - but each to their own.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    22
    Wow. Good catch on the MessageLookupTable. I had a logic error in its search loop that cause it the method that looks up the message handlers was looping though the entire table for EACH message. That explains the stalling!

    If I declare/register/create the window the 'C' way (not using my wrapper class), the window is created an displayed just fine. When I use my wrapper class, something funny happens to hWnd so that it either becomes NULL (if I use hWnd = ::CreateWindow(blah...)) or it gets a some other hWnd (from the currentWindow ->SetWindowHandle(hWnd) in the handler for WM_NCCREATE || WM_CREATE) that does not display a window.

    At least now I have something to work with. Thanks.
    Never give an Ewok a thermial detonator

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    16
    Do you call your ShowWindow wrapper alot? Or do you only call it right after window creation? If that's the case, then you can save yourself the ShowWindow/UpdateWindow call by Creating the window with the WS_VISIBLE style. Just force it in your CreateWindow wrapper by or'ing this style with the style passed to it.

    --Chorus

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    22
    Actually, I'm not sure how much we'll be using ShowWindow (also, I actually got everything to place nice together in a C++ wrapper). This is the foundation for group senior year college project that will display a 3D database (one of those "start in the deep end" learning experiences). If it turns out that show won't be used (no Alt-Tabbing), I'll being using the method you suggested.
    Never give an Ewok a thermial detonator

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do the Access Violations every end?
    By durban in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2005, 10:13 PM
  2. Access Violations
    By nickname_changed in forum C++ Programming
    Replies: 0
    Last Post: 09-22-2003, 03:49 AM
  3. Finding access violations in the source
    By Carlos in forum Windows Programming
    Replies: 4
    Last Post: 11-20-2002, 04:07 AM
  4. Replies: 2
    Last Post: 06-13-2002, 11:05 PM
  5. access violations
    By curtner in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 01:32 AM