What is the nicest way to close FreeGLUT?

freeglut documentation
freeglut example
freeglut create window
glut documentation pdf
glutget glut_version

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
        close(); // close function if coming here somehow
        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)
        // Handle the CTRL-C signal.
        case CTRL_C_EVENT:
        // and the close button
        case CTRL_CLOSE_EVENT:
          return true;

        // Pass other signals to the next handler. 
        case CTRL_BREAK_EVENT:
            return false;

    // delete the pointer anyway
        case CTRL_LOGOFF_EVENT:
            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++.


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?

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.


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

API Documentation, 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,  Users looking to prevent FreeGLUT from exiting when a window is closed, should look into using glutSetOption to set GLUT_ACTION_ON_WINDOW_CLOSE. GLUT_ACTION_EXIT will immediately exit the application (default, GLUT's behavior). GLUT_ACTION_GLUTMAINLOOP_RETURNS will immediately return from the main loop.

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"

OpenGL SuperBible: Comprehensive Tutorial and Reference, There was no way for an application to loop in GLUT for a while, possibly as a The second, glutLeaveMainLoop, causes the event loop to exit nicely; this is  freeGLUT keyboard input. Ask Question What is the nicest way to close FreeGLUT? 3. Is there a way inserts exit callback function in freeglut (or glut)? 0.

I use glutDestroyWindow(int handle);


According to ID: RigidBody at OpenGL forum

void destroy_window() 
 window_valid = -1;

void display_func() 
 if(window_valid == -1)
 // draw things

freeglut / Re: [Freeglut-developer] glutCloseFunc and glutmainloop, This helps to make any applications that use GLUT very portable given that the code can be compiled on Windows, Mac, and Linux. It is also a good way to get  glutCreateWindow on multi gpu system. What is the nicest way to close FreeGLUT? 2. Getting a return value from a Freeglut/Opengl 2.1 project. 3.

Try this method:


Is FreeGLUT still a good choice? [closed], If not, I'll need to find another way to do it, perhaps with GTK. I think I don't properly close the Freeglut Window, and this is the >> > cause of the errors Managed Hosting >> > A question and answer guide to determining the best fit >​> > for  2 Answers 2. FreeGLUT offers several solutions to that: You can call glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION) and glutLeaveMainLoop() to make glutMainLoop() function return, and then you can free all the memory you want after glutMainLoop() call.

Computer Graphics Through OpenGL®: From Theory to Experiments, I would only recommend using FreeGLUT if you are just learning OpenGL and don't want to worry yourself with things like window and context  @CroCo, any other solution than freeglut is welcome. closing window by clicking on x mark is totally possible. while I am looking for a way to close the window by the code rather than by user. – ar2015 Jan 2 '17 at 23:38

FREEGLUT Application Program Interface, OpenGL is a powerful and sophisticated API for creating 3D graphics, with of course, some other very good platform independent abstractions of this sort, too,​  Freeglut allows the programmer to force this by setting the GLUT_ACTION_ON_WINDOW_CLOSE option and invoking the glutLeaveMainLoop function from one of the callbacks. Stopping the program this way is preferable to simply calling exit from within a callback because this allows freeglut to free allocated memory and otherwise clean up after itself.

Learning Game Physics with Bullet Physics and OpenGL, End. 2.12. Summary,. Notes. and. More. Reading. In this chapter we began We discovered as well how OpenGL functions as a state machine, attributes such as color defining the current state. (That no one owns OpenGL is a good thing.)  Test your project. Right click on following link and select Open Link in New Window badprog. Scroll down the page and find section Testing the setup (see image above). Copy code and paste in Main.cpp code area > hit Ctrl + F5. You should see two windows: one black (the console) and other with white square in black background. If you do not see

  • You don't need to worry about freeing memory when your program shuts down, the OS reclaims everything and nothing leaks. Persistent resources like files should still be cleaned up properly of course.
  • atexit() probably won't solve your problem of C++ objects cleanup.
  • @Ben, @wilhelmtell: But I should clear my keyboardManager object for example, because my Progam holds a pointer to it, right?
  • If it uses some resources that survive the termination of your program, yes. But the OS will reclaim memory and should also revoke keyboard input capture, so a keyboard manager probably doesn't need cleanup. There's even a school of thought that says not to clean up persistent resources either, the rationale being that eventually your cleanup won't get a chance to run (catastrophic power failure perhaps), so you need logic to recover dirty state. And if you can deal with dirty state, then any cleanup is just a waste of time.
  • If you care about opengl leaks (say you're recreating things or you startup and tear down OpenGL more than once) then you can use GDebugger to report all leaked OpenGL objects. It also reports all sorts of useful things that you may be doing incorrectly, like redundant state changes, or using deprecated functionality.
  • Seems indeed interesting. So I could call this function in the SetConsoleCtrlHandler and in the keychecker to check ESC. So I could then put my pointer deleters in the main function probably?
  • It is far better to setup glutCloseFunc to call a function that cleans up. FreeGLUT will call it before it tries to tear down the context, unlike doing it after the mainloop.
  • Okay so according to the latest GLUT specification (3.7), glutLeaveMainLoop() does not exist anymore. So now what would be the answer to the OP's question?
  • glutLeaveMainLoop still exists within FreeGLUT (and I believe OpenGLUT, though unverified, but their subwin sample uses it). Tested with latest stable version, 3.0.0 - it's an extension to the regular GLUT.
  • Ubuntu has it but there's a trick. GLUT on Ubuntu is all FreeGLUT, and the FreeGLUT is split into _std and _ext header files. If you include GL/glut.h it just includes freeglut_std.h. You have to include GL/freeglut.h instead, and that includes both the _std and freeglut_ext.h headers, and then you get glutLeaveMainLoop().