0
votes

I'm trying to create a simple OpenGL context but the program crashes on ChoosePixelFormat with the error "Unhandled exception at 0x779CE0E6 (ntdll.dll) in OpenGL.exe: 0xC0000005: Access violation reading location 0x00000044." This same code used to work a while back but for some reason doesn't work anymore. I tried updating my graphics drivers to no avail. If it matters, I have a 64-bit Windows 7 Home premium, a GeForce 570 and a Intel Core i7-2600 3,40 GHz.

This is the code I use in the order of execution to the point it crashes:

GLEngine gl(WndProc); //WndProc just calls DefWindowProc
- calls ->
GLEngine::GLEngine(WNDPROC wndproc) { //Initialize class

    hRC = NULL;
    hDC = NULL;
    hWnd = NULL;
    fullscreen = false;
    active = false;

    proc = wndproc;
    itemsLength = 0;

    currentActive = this;

    success = true;
}

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR    lpCmdLine, _In_ int       nCmdShow) {

    gl.CreateGL2Window("Test", 1300, 900, 8, false, true);

- calls ->

bool GLEngine::CreateGL2Window(char* title, int width, int height, bool internalflag) {

    GLuint pixelFormat;
    WNDCLASSEX wc;

    DWORD dwExStyle;
    DWORD dwStyle;

    RECT windowRect;
    windowRect.left = (long)0;
    windowRect.right = (long)width;
    windowRect.top = (long)0;
    windowRect.bottom = (long)height;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance; //hInstance is NULL here, should it be something else?
    wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = _T("OpenGL");
    wc.hIconSm = NULL;

    if(!RegisterClassEx(&wc)) {

        MessageBox(NULL, _T("Failed To Register The Window Class."), _T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    hInstance = wc.hInstance; //hInstance isn't NULL anymore

    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    dwStyle = WS_OVERLAPPEDWINDOW;

    AdjustWindowRectEx(&windowRect, dwStyle, false, dwExStyle);

    if(!(hWnd = CreateWindowEx(dwExStyle, _T("OpenGL"), 
        (wchar_t*)title, 
        WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
        0, 0,
        windowRect.right - windowRect.left,
        windowRect.bottom - windowRect.top,
        NULL,
        NULL,
        hInstance,
        NULL))) {
            DestroyGLWindow();
            MessageBox(NULL, _T("Window Creation Error."), _T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
            return false;
    }

    if(!(hDC = GetDC(hWnd))) {

        DestroyGLWindow();
        MessageBox(NULL, _T("Can't Create A GL Device Context."), _T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
        return false;
    }

    PIXELFORMATDESCRIPTOR pfd2 = { sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        32,
        0, 0, 0, 0, 0, 0,
        0,
        0,
        0,
        0, 0, 0, 0,
        32,
        8,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0
    };
    if(!(pixelFormat = ChoosePixelFormat(hDC, &pfd2)) {

GLEngine is in a dll. It shouldn't matter but the information can't hurt.

1
Just tested your code, doesn't throw any errors for me (except of a missing ) in the last if-statement. - BDL
It is a simple null pointer exception. Probably dies on trying to access the hDC member. So it looks like it called ChoosePixelFormat() but it didn't quite get there yet. Should be obvious from the stack trace, you'd see CreateGL2Window but not ChoosePixelFormat. And the debugger, set a breakpoint and inspect this. How gl got to be corrupted is hard to see, perhaps you are shadowing it. - Hans Passant
@HansPassant I've put a breakpoint on the ChoosePixelFormat line and it crashes on that line. Also checked that neither hDC or &pfd2 are null. Call stack just shows different dlls greyed out. because I don't have the symbols for them. (ntdll.dll and appinit_dll.dll) - Hoxy
@BDL What graphics card/drivers do you have? - Hoxy

1 Answers

0
votes

Did you get address of function with GetProcAddress? Faulty drivers can cause crash on this too. ChoosePixelFormat may be implemented and exported driver-side, but it might be not exported, then system procedure should be used. exported one may or may not filling descriptor structure, depending on implementation, so you need to call DescribePixelFormat.