关闭 FreeGLUT 的最好方法是什么?

发布于 2024-10-18 02:23:32 字数 1761 浏览 2 评论 0原文

我在使用 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; 
    } 
}

所以正确的是:

  1. 关闭 glut 窗口
  2. 使用 x 关闭控制台应用程序
  3. 使用键盘管理器关闭 glut 窗口 if(keyboardManager->isKeyDown[27]) glutExit() ;

错误是:

  1. 使用 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:

  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();

What goes wrong is:

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鯉魚旗 2024-10-25 02:23:32

我使用这个功能:

void glutLeaveMainLoop ( void ); 

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

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

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

在空指针上使用delete是安全的,无需检查。

I use this function:

void glutLeaveMainLoop ( void ); 

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

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

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

早乙女 2024-10-25 02:23:32

感谢 Maarten 的帖子,这对我有用:

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);

每当你想在没有 termiante 的情况下离开主循环时,应用程序使用:

glutLeaveMainLoop();

不要忘记包含“freeglut.h”

Thanks to Maarten's post, this works to me:

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);

whenever you want to leave the mainloop without termiante the application use:

glutLeaveMainLoop();

don't forget to include "freeglut.h"

dawn曙光 2024-10-25 02:23:32

我使用 glutDestroyWindow(int handle);

根据 ID:OpenGL 论坛的 RigidBody

void destroy_window() 
{
 window_valid = -1;
}

void display_func() 
{
 if(window_valid == -1)
   return;
 // draw things
}

I use glutDestroyWindow(int handle);

or

According to ID: RigidBody at OpenGL forum

void destroy_window() 
{
 window_valid = -1;
}

void display_func() 
{
 if(window_valid == -1)
   return;
 // draw things
}
掐死时间 2024-10-25 02:23:32

试试这个方法:

glutDestroyWindow(glutGetWindow());

Try this method:

glutDestroyWindow(glutGetWindow());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文