Search code examples
openglglutfreeglut

Is there a way inserts exit callback function in freeglut (or glut)?


In my freeglut project, i have allocated alot of memory, and i have no ways to free that when users close freeglut (or glut) window, any ideas?


Solution

  • FreeGLUT offers several solutions to that:

    1. 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.

    2. Instead of calling glutMainLoop() you can make your own event loop like this:

      bool running = true;
      while (running)
      {
          glutMainLoopEvent();
      }And whenever you want to exit application - just set running variable to false, and free the allocated memory after while loop.
      
    3. Or you can do nothing - any modern OS correctly deallocates all the allocated memory when process terminates. Of course, if you need do some special thing on termination - like write to log file, send network packets, then you must manually do that.