Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the nicest way to close 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:

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?

like image 976
Marnix Avatar asked Feb 17 '11 19:02

Marnix


2 Answers

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.

like image 171
Maarten Avatar answered Oct 08 '22 02:10

Maarten


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
}
like image 44
Cloud Cho Avatar answered Oct 08 '22 03:10

Cloud Cho