title: Freeglut error: ERROR: No display callback registered for window 1 when destroyed a window and created a new window
body: <p>I want to create the opengGL context using freeglut. I will first decide the which context to by checking the supporting version using glew and some other parameters. I know for glew to work, it needs a opengl context. So I first create a context using glutCreateWindow, then check the supported version and then set the version required using the glutInitContextVersion() and destroy the previous window using glutDestroyWindow and recreate the new window by using glutCreateWindow. I get this error  <b> Freeglut error: ERROR:  No display callback registered for window 1 (I checked 1 is ID for my previous window which I destroyed) </b>. Following is my code</p>
  
  <pre><code>glutInit(&amp;argc, argv);
  glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  glutInitWindowPosition(100, 100);
  glutInitWindowSize(900, 600);
  
  int winID = glutCreateWindow("Rotating Cube"); //winID is 1 here
  
  glewExperimental = GL_TRUE;
  glewInit();
  
  //I decide the context on some other parameters also except the supported version reported by glew. 
  //I have tested this with opengl 3.2 core profile as well. 
  //This is not working even if I forcefully set the opengl version to 2.0
  if (glewIsSupported("GL_VERSION_3_1"))
  {
      glutInitContextVersion (3, 1);
      glutInitContextFlags (GLUT_FORWARD_COMPATIBLE);
  
      //glutInitContextVersion (3, 2);             
      //glutInitContextFlags (GLUT_CORE_PROFILE);
  }
  else if (glewIsSupported("GL_VERSION_2_0"))
  {
      glutInitContextVersion (2, 0);
  }
  
  glutDestroyWindow(winID);
  winID = glutCreateWindow("Rotating Cube"); //winID is 2 here
  
  glutSetWindow(winID);
  
  glutDisplayFunc(RenderScene);
  glutIdleFunc(RenderScene);
  glutReshapeFunc(ChangeSize);
  glutKeyboardFunc(ProcessNormalKeys);
  glutSpecialFunc(ProcessSpecialKeys);
  
  glutMainLoop();
  </code></pre>
  
  <p>I think I need to do this as a openGL context is always required for glew to work. I already tried setting the display function on first window as well (though I know I am going to destroy it) but that also didn't work. I am setting the current window to new window and then calling the glutMainLoop. So I think this should work</p>
  
  <p>According to answer by rhashimoto, I tried to put the destroy command at different positions</p>
  
  <pre><code>if (g_winID&gt;=0)
  {
      glutDestroyWindow(g_winID);
      g_winID = -1;
  }
  </code></pre>
  
  <p>I put the destroy command at begining of reshape callback</p>
  
  <pre><code>ERROR:  No display callback registered for window 1
  </code></pre>
  
  <p>The I put the destroy command at beginning of display function</p>
  
  <pre><code>ERROR:  Function &lt;glutSwapBuffers&gt; called with no current window defined.
  </code></pre>
  
  <p>If I put this at end of Display callback, it does not give error but the displayed scene is not correct. Somethings are missing from the scene</p>
  
  <p>So is there some specific callback function I need to put this display command? I dont think there is any destroy callback I can set. yes there is <code>glutCloseFunc</code> but I think that is meant to be called when window is being destroyed that is when <code>glutDestroyWindow</code> has been called on window</p>