I fell for it from time to time, trying to play with GLUT. I tried everything I could, including IIRC coming out of glutMainLoop through an exception caught in the main function, but ...
When using glutMainLoop
My solution was this: Create a global Context object that will be the owner of all your resources and free those resources in the destructor.
This global destructor of the Context object is called immediately after exiting the main one.
Important Context is a global variable, not a variable declared in the main function, because for the reason that it still eludes me (I still donβt see interest in this implementation option), glutMainLoop will not return.
In my Linux box (Ubuntu), the destructor is called correctly. I think it will also work on Windows and MacOS.
Please note that this is the C ++ version of the Francisco Soto atexit() version without any restrictions.
Using glutMainLoopEvent
Apparently, some implementations have glutMainLoopEvent, which can be used instead of calling glutMainLoop.
http://openglut.sourceforge.net/group__mainloop.html#ga1
glutMainLoopEvent only allow pending events, and then will return. Thus, you should specify the event loop ( for(;;) construct) around the glutMainLoopEvent call, but in this way you can work with GLUT and still have control over the event loop and free up your resources when necessary.
paercebal
source share