ChoosePixelFormat Access violation reading location

0

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.

c++
windows
opengl
asked on Stack Overflow Feb 20, 2017 by Hoxy

1 Answer

0

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.

answered on Stack Overflow Feb 20, 2017 by Swift - Friday Pie

User contributions licensed under CC BY-SA 3.0