What is the nicest way to close FreeGLUT?

2019-02-09 00:07发布

问题:

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?

回答1:

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.



回答2:

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"



回答3:

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
}


回答4:

Try this method:

glutDestroyWindow(glutGetWindow());