title: What is the nicest way to close FreeGLUT?
body: <p>I'm really having trouble closing my console application with FreeGLUT.</p>
  
  <p>I would like to know what the best way is to take every possible closing, because I don't want any memory leaks (I'm pretty afraid of those).</p>
  
  <p>So I already tried the following, which is giving me an exception like this:</p>
  
  <blockquote>
    <p>First-chance exception at 0x754e6a6f in myProject.exe: 0x40010005: Control-C.</p>
  </blockquote>
  
  <pre><code>int main(int argc, char **argv)
  {
      if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, true) )
      {
          // more code here as well ....
  
  
          glutCloseFunc(close); // set the window closing function of opengl
          glutMainLoop();
          close(); // close function if coming here somehow
      }
      else
      {
          return 1;
      }
      return 0;
  }
  
  void close()
  {
      // keyboardManager is a pointer to a class
      // which I want to delete, so no memory will leak.
      if(keyboardManager) // do I need this check?
          delete keyboardManager;
  }
  
  bool CtrlHandler(DWORD fdwCtrlType)
  {
      switch(fdwCtrlType)
      {
          // Handle the CTRL-C signal.
          case CTRL_C_EVENT:
          // and the close button
          case CTRL_CLOSE_EVENT:
            close();
            return true;
  
          // Pass other signals to the next handler. 
          case CTRL_BREAK_EVENT:
              return false;
  
      // delete the pointer anyway
          case CTRL_LOGOFF_EVENT:
          case CTRL_SHUTDOWN_EVENT:
          default:
              close();
              return false; 
      } 
  }
  </code></pre>
  
  <p>So what goes right is:</p>
  
  <ol>
  <li>Closing the window of glut</li>
  <li>Closing the console application with the <code>x</code></li>
  <li>Closing my window of glut with my keyboardmanager <code>if(keyboardManager-&gt;isKeyDown[27]) glutExit();</code></li>
  </ol>
  
  <p>What goes wrong is:</p>
  
  <ol>
  <li>Closing the console application with CTRL+C, it gives the exception from above.</li>
  </ol>
  
  <p>This is in Visual Studio 2008 C++.</p>
  
  <p><em>UPDATE</em></p>
  
  <p>I found that the exception is thrown, because I'm in debug. So that won't be a problem. But the question is still open: <em>What is the most elegant way to actually close glut?</em></p>
  
  <p><code>atexit()</code> seems to work as well, so maybe I can use this?</p>