什么是最好的方式来关闭FreeGLUT? [英] What is the nicest way to close FreeGLUT?

查看:685
本文介绍了什么是最好的方式来关闭FreeGLUT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:


myProject.exe中0x754e6a6f的第一次例外:0x40010005:Control-C。

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. 用键盘管理器关闭我的窗口 if(keyboardManager-> isKeyDown [27])glutExit();

  1. Closing the window of glut
  2. Closing the console application with the x
  3. Closing my window of glut with my keyboardmanager if(keyboardManager->isKeyDown[27]) glutExit();

出现错误的是:



这是在Visual Studio 2008 C ++中。

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 / code>似乎也工作,所以也许我可以使用这个?

atexit() seems to work as well, so maybe I can use this?

推荐答案

/ p>

I use this function:

void glutLeaveMainLoop ( void ); 

有关他们的sourceforge页面的更多信息,但我从来没有使用该功能:

There is more information on their sourceforge page but I never used that functionality:


glutLeaveMainLoop函数导致freeglut停止事件循环。如果GLUT_ACTION_ON_WINDOW_CLOSE选项设置为GLUT_ACTION_CONTINUE_EXECUTION,控制将返回调用glutMainLoop的函数;否则应用程序将退出。

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 一个空指针,不需要检查。

It is safe to use delete on a null pointer, no need to check.

这篇关于什么是最好的方式来关闭FreeGLUT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆