gpt4 book ai didi

c++ - 关闭 FreeGLUT 的最佳方式是什么?

转载 作者:可可西里 更新时间:2023-11-01 15:03:51 26 4
gpt4 key购买 nike

我真的很难用 FreeGLUT 关闭我的控制台应用程序。

我想知道最好的方法是采取每一个可能的关闭,因为我不希望任何内存泄漏(我非常害怕那些)。

所以我已经尝试了以下方法,这给了我这样的异常:

First-chance exception at 0x754e6a6f in myProject.exe: 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;
}
}

那么正确的做法是:

  1. 关闭供过于求的窗口
  2. 使用 x
  3. 关闭控制台应用程序
  4. 用我的键盘管理器关闭我的过剩窗口 if(keyboardManager->isKeyDown[27]) glutExit();

问题是:

  1. 使用 CTRL+C 关闭控制台应用程序,它给出了上面的异常。

这是在 Visual Studio 2008 C++ 中。

更新

我发现异常被抛出,因为我在调试中。所以这不会有问题。但问题仍然悬而未决:真正解决过剩问题的最优雅方法是什么?

atexit() 似乎也能正常工作,也许我可以使用它?

最佳答案

我使用这个函数:

void glutLeaveMainLoop ( void ); 

他们的 sourceforge 页面上有更多信息,但我从未使用过该功能:

The glutLeaveMainLoop function causes freeglut to stop the event loop. If the GLUT_ACTION_ON_WINDOW_CLOSE option has been set to GLUT_ACTION_CONTINUE_EXECUTION, control will return to the function which called glutMainLoop; otherwise the application will exit.

http://freeglut.sourceforge.net/docs/api.php#EventProcessing

对空指针使用delete是安全的,不需要检查。

关于c++ - 关闭 FreeGLUT 的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5033832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com