Search code examples
c++glutfreeglut

illegal glutInit() reinitialization attempt: How can I de-initialize glut?


I implemented a class function that call glutInit() if the class is activated. Like this:

void myViewer::Activate() {
  int argc = 1;
  char* argv[1] = {const_cast<char*>("none")};
  glutInit(&argc, argv);
}
void myViewer::Deactivate() {
  ...
}

How can I "de-initialize" the Glut component? Calling the activation function twice throws an error freeglut (none): illegal glutInit() reinitialization attempt


Solution

  • The Initialization section of the documentation clearly states:

    The primary initialization routine is glutInit that should only be called exactly once in a GLUT program.

    You are also not using glutInit as specified. Trying to provide fake arguments is asking for trouble. They want the original (argc, argv) for a reason:

    glutInit will initialize the GLUT library and negotiate a session with the window system.

    Now for a technical reason, closing an OpenGL session on the fly is not a simple task. It requires a careful and thorough shutdown of all OpenGl activities before trying to release the display surface negotiated with the operating system.

    Many video games don't even try to do it. They simply grab a window for display and run until the game terminates (one way or another :) ).

    And as it happens, glut explicitly forbids you to try.