Bình Nguyên
Bình Nguyên

Reputation: 2352

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?

Upvotes: 2

Views: 4491

Answers (2)

Spongebob Comrade
Spongebob Comrade

Reputation: 1568

Register an exit function with atexit(onexit) before the main loop.

Upvotes: 2

Mārtiņš Možeiko
Mārtiņš Možeiko

Reputation: 12927

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.

Upvotes: 8

Related Questions