How to kill processes before closing the window (using GLUT && C++)

I have the class X. I want to destruct it when user click [_] [x]-here (close window button). What should I do? I need it, because I mmap something in class X and I need to munmap it while exit.


X *myclass;

int main(int argc, char** argv) {
myclass = new X();

// Some glut stuff…


return 1;

How to delete myclass or destruct it (myclass->~X) while exit? Where?


I use GLUT thread and create new window with GLUT too. TNX!

Try the atexit() function.

#include <stdlib.h>
void myAtExit()
    // destroy stuff ...
int main(...)
    atexit( myAtExit );
    return 0;


I see that this has been answered in another forum.