无法释放设备上下文,HWND,并且无法取消注册Windows类(OpenGL) -

时间:2014-12-29 13:21:06

标签: c++ opengl hwnd device-context hinstance

我正在关注http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/ OpenGL教程,我从那里得到了代码。现在,我试图通过使用多个类来组织事物。当我创建这个类时,我无法释放设备上下文,HWND,我无法取消注册Windows类。下面的代码是用于检查是否可以释放它们的代码:

GLvoid KillGLWindow(GLvoid){ 
if (fullscreen){
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(true);
}
if (hRC){
    if (!wglMakeCurrent(NULL, NULL)){
        MessageBox(NULL, "Release of DC and RC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    }
    if (!wglDeleteContext(hRC)){
        MessageBox(NULL, "Release of RC failed", "Shutdown error", MB_OK | MB_ICONEXCLAMATION);
    }
    hRC = NULL;
}
if (hDC && !ReleaseDC(hWnd, hDC)){
    MessageBox(NULL, "Release of DC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hDC = NULL;
}
if (hWnd && !DestroyWindow(hWnd)){
    MessageBox(NULL, "Release of hWnd failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hWnd = NULL;
}
if (!UnregisterClass("OpenGL", hInstance)){
    MessageBox(NULL, "Could not unregister window class", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hInstance = NULL;
}
}

(最后三个如果法庭被解雇)

我正在移动的代码导致这些错误是WinMain函数中的关键检测代码。这是我改变的唯一代码。

else{
        if (active){

            if (testKey.isEsc()){
                done = true;
            }
            if (testKey.isA()){
                KillGLWindow();
            }
            else{
                DrawGLScene();
                SwapBuffers(hDC);
            }
        }
            if (testKey.isF1()){
                //Keys::keys[VK_F1] = false;
                KillGLWindow();
                fullscreen = !fullscreen;
                if (!CreateGLWindow("XcoxGL", 640, 480, 16, fullscreen)){
                    return 0;
                }
            }

我改变的是testKey.THING部分。 testKey在主类中由行

启动
Keys testKey

Keys.cpp看起来像这样:

bool Keys::keys[256] = { false };

bool Keys::isA(){
    if (&keys[0x41]){
        return true;
    }
    else{
        return false;
    }
}

bool Keys::isF1(){
    if (&keys[VK_F1]){
        return true;
    }
    else{
        return false;
    }
}

bool Keys::isEsc(){
    if (&keys[VK_ESCAPE]){
        return true;
    }
    else{
        return false;
    }
}

最后,Keys.h看起来像这样:

#pragma once
class Keys{
public:
    static bool keys[256];
     bool isA();

     bool isF1();

     bool isEsc();
};

如果您愿意,我可以发布完整的代码,但是我在上面发布的教程中显示并解释了创建DC和HWND的方式。

有没有人知道我的密码中是什么导致我的DC和HWND无法释放?

1 个答案:

答案 0 :(得分:0)

可能KillGLWindow不止一次被调用,而且不是很合适,试试这个:

GLvoid KillGLWindow(GLvoid){ 
if (fullscreen){
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(true);
}
if (hRC){
    if (!wglMakeCurrent(NULL, NULL)){
        MessageBox(NULL, "Release of DC and RC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    }
    if (!wglDeleteContext(hRC)){
        MessageBox(NULL, "Release of RC failed", "Shutdown error", MB_OK | MB_ICONEXCLAMATION);
    }
}
hRC = NULL;
if (hDC && !ReleaseDC(hWnd, hDC)){
    MessageBox(NULL, "Release of DC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hDC = NULL;
if (hWnd && !DestroyWindow(hWnd)){
    MessageBox(NULL, "Release of hWnd failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hWnd = NULL;
if (hInstance && !UnregisterClass("OpenGL", hInstance)){
    MessageBox(NULL, "Could not unregister window class", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hInstance = NULL;
}