Garbage Cleanup with glut??

I just realized that the clean up function in my code is not getting called upon exit (I didn’t realize glutMainLoop() did not return at all – I figured it might return upon closing the program). I’ve looked through the glut manual and there doesn’t appear to be a way to register a call back function to run clean up code upon exiting. My C++ program uses several global objects that are dynamically allocated, and without being able to trap an exit event or register a clean up function, I can’t think of any way to free these objects aside from reading through the glut source code, trying to figure out what events are blasted out on exit, spinning up a separate thread in my main code to try to trap this event, and then run the clean up code from there. Am I missing something? Any ideas or suggestions on what to do?

GLUT uses the atexit() philosophy. It’s a standard C function that registers a function that is called upon exit() (which is what GLUT does when a GLUT window is closed, for instance). Google it or check your C language books.

As marcus256 said, atexit() is pretty much your only option. Even then you may have problems - if your app isn’t using the same C runtime library as GLUT, your atexit handlers won’t get called. See the comment in glut.h (around line 75 in my version) for details.

On the plus side, if it’s ONLY memory allocations you need to clean up, you don’t really need to bother. The OS will clean up all memory used by the process when it exits. It’s not very elegant, and certainly not a good habit to get into, but… shrug