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.

Example:

X *myclass;

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

//…
// Some glut stuff…
//…

glutMainLoop();

return 1;
}

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

P.S.

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 );
    glutMainLoop();
    return 0;
}
 

edit:

I see that this has been answered in another forum.