关闭 FreeGLUT 的最好方法是什么?
我在使用 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;
}
}
所以正确的是:
- 关闭 glut 窗口
- 使用
x
关闭控制台应用程序 - 使用键盘管理器关闭 glut 窗口
if(keyboardManager->isKeyDown[27]) glutExit() ;
错误是:
- 使用 CTRL+C 关闭控制台应用程序,它给出了上面的异常。
这是在 Visual Studio 2008 C++ 中。
更新
我发现抛出了异常,因为我处于调试状态。所以这不会成为问题。但问题仍然悬而未决:真正关闭过剩的最优雅的方法是什么?
atexit()
似乎也有效,所以也许我可以使用这个?
I'm really having trouble closing my console application with FreeGLUT.
I would like to know what the best way is to take every possible closing, because I don't want any memory leaks (I'm pretty afraid of those).
So I already tried the following, which is giving me an exception like this:
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;
}
}
So what goes right is:
- Closing the window of glut
- Closing the console application with the
x
- Closing my window of glut with my keyboardmanager
if(keyboardManager->isKeyDown[27]) glutExit();
What goes wrong is:
- Closing the console application with CTRL+C, it gives the exception from above.
This is in Visual Studio 2008 C++.
UPDATE
I found that the exception is thrown, because I'm in debug. So that won't be a problem. But the question is still open: What is the most elegant way to actually close glut?
atexit()
seems to work as well, so maybe I can use this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用这个功能:
他们的 sourceforge 页面上有更多信息,但我从未使用过该功能:
http://freeglut.sourceforge.net/docs/api.php#EventProcessing
在空指针上使用
delete
是安全的,无需检查。I use this function:
There is more information on their sourceforge page but I never used that functionality:
http://freeglut.sourceforge.net/docs/api.php#EventProcessing
It is safe to use
delete
on a null pointer, no need to check.感谢 Maarten 的帖子,这对我有用:
每当你想在没有 termiante 的情况下离开主循环时,应用程序使用:
不要忘记包含“freeglut.h”
Thanks to Maarten's post, this works to me:
whenever you want to leave the mainloop without termiante the application use:
don't forget to include "freeglut.h"
我使用
glutDestroyWindow(int handle);
或
根据 ID:OpenGL 论坛的 RigidBody
I use
glutDestroyWindow(int handle);
or
According to ID: RigidBody at OpenGL forum
试试这个方法:
Try this method: