我真的很难用 FreeGLUT 关闭我的控制台应用程序。
我想知道最好的方法是采取一切可能的关闭方式,因为我不想要任何内存泄漏(我很害怕那些)。
所以我已经尝试了以下方法,这给了我这样的例外:
myProject.exe 中 0x754e6a6f 处的第一次机会异常:0x40010005:Control-C。
int main(int argc, char **argv)
{
if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, true) )
{
// more code here as well ....
glutCloseFunc(close); // set the window closing function of opengl
glutMainLoop();
close(); // close function if coming here somehow
}
else
{
return 1;
}
return 0;
}
void close()
{
// keyboardManager is a pointer to a class
// which I want to delete, so no memory will leak.
if(keyboardManager) // do I need this check?
delete keyboardManager;
}
bool CtrlHandler(DWORD fdwCtrlType)
{
switch(fdwCtrlType)
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
// and the close button
case CTRL_CLOSE_EVENT:
close();
return true;
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
return false;
// delete the pointer anyway
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
default:
close();
return false;
}
}
所以正确的是:
- 关闭过剩之窗
- 关闭控制台应用程序
x
- 用我的键盘管理器关闭我的过剩窗口
if(keyboardManager->isKeyDown[27]) glutExit();
出问题的是:
- 使用 CTRL+C 关闭控制台应用程序,它会从上面给出异常。
这是在 Visual Studio 2008 C++ 中。
更新
我发现抛出了异常,因为我在调试。所以这不会是一个问题。但问题仍然悬而未决:真正关闭过剩的最优雅方式是什么?
atexit()
似乎也可以,所以也许我可以使用它?