title: intermittent GLSL vertex shader compile errors
body: <p>I've been getting intermittent errors compiling a vertex shader, preparing for the first render of a newly created OpenGL context. It's the same vertex shader that usually works, on the same hardware. After the failure, the info log returned by <code>glGetShaderInfoLog</code> usually shows this:</p>
  
  <pre><code>Vertex shader failed to compile with the following errors:
  </code></pre>
  
  <p>Yes, that's the whole thing. It happens on Windows, with both ATI and NVidia GPUs, though I've mostly been testing on ATI.  If I'm running under the Visual Studio debugger, an access violation may be detected in <code>glCompileShader</code> or in the subsequent <code>glGetShaderiv</code> call.</p>
  
  <p>I haven't seen the bug on the Mac, but since it's not always easy to reproduce it, I'm not absolutely sure it doesn't happen.</p>
  
  <p>I have noticed that if I don't call <code>wglShareLists</code> when I create my contexts, the error goes away (or at least I can't reproduce it easily).  I take that to mean that some bad information is bleeding through from a previously created (and perhaps previously destroyed) OpenGL context to the new one.  However, I've been trimming out stuff until there is not much that should be shared: No texture objects, display lists, VBOs, PBOs. The only things left that would be shared, as far as I know, would be shader and program objects.</p>
  
  <p>After creating an OpenGL context for a window, I initialize shading roughly like this:</p>
  
  <pre><code>// Program initialization
  GLint vertexShader = glCreateShader( GL_VERTEX_SHADER );
  glShaderSource( vertexShader, 1, &amp;vertexShaderSource, NULL );
  glCompileShader( vertexShader );
  GLint   status;
  glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &amp;status );
  if (status == GL_TRUE)
  {
      GLint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
      glShaderSource( fragmentShader, 1, &amp;fragShaderSource, NULL );
      glCompileShader( fragmentShader );
      glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &amp;status );
      if (status == GL_TRUE)
      {
          GLint program = glCreateProgram();
          if (program != 0)
          {
              glAttachShader( program, vertexShader );
              glAttachShader( program, fragmentShader );
              glLinkProgram( program );
              glDetachShader( program, fragmentShader );
              glDetachShader( program, vertexShader );
              glDeleteShader( fragmentShader );
              // check for link success...
          }
      }
  }
  else
  {
      char logBuf[1024];
      glGetShaderInfoLog( vertexShader, sizeof(logBuf), NULL, (GLchar*)logBuf );
      LogToFile( (char*)logBuf );
  }
  </code></pre>
  
  <p>Then I render multiple frames, and at some point clean up</p>
  
  <pre><code>glDeleteProgram( program );
  glDeleteShader( vertexShader );
  </code></pre>
  
  <p>and destroy the OpenGL context.  Later, I create a new OpenGL context in the same window, initialize it in the same way, but the vertex shader fails to compile.</p>
  
  <p>If I don't render any geometry, I can't get the bug to reproduce, but rendering a single point suffices.</p>
  
  <p>It still happens with a simplified vertex shader:</p>
  
  <pre><code>#version 120
  void main()
  {
      gl_Position = ftransform();
      gl_FrontColor = gl_Color;
      gl_BackColor = gl_Color;
  }
  </code></pre>
  
  <p>and a simplified fragment shader:</p>
  
  <pre><code>#version 120
  void main()
  {
      gl_FragColor = gl_Color;
  }
  </code></pre>
  
  <p>I tried AMD's CodeXL OpenGL debugger, setting it to break on errors.  It just tells me that the compile is failing, which I already knew.</p>
  
  <p>The OpenGL context that fails is on a window where a different context was previously created, successfully rendered, and destroyed.  There's nothing wrong with re-using a window like that, is there?  (I am aware that once you've called <code>SetPixelFormat</code>, you can't change the pixel format of the window.  I made sure that I'm using the same pixel format each time.)</p>
  
  <p>ADDED:  While trimming down the rendering code, I found that if I commented out the line</p>
  
  <pre><code>glEnable( GL_VERTEX_PROGRAM_TWO_SIDE );
  </code></pre>
  
  <p>then the error went away.  But with more real-world rendering (e.g., textures) reinstated, it doesn't seem to make a difference.</p>
  
  <p>ADDED 3/7/2014:  I finally made a self-contained Visual C++ project that can reproduce the bug under certain circumstances, so that any interested parties can try it:  <a href="http://www.frameforge3d.com/news-mac/ShaderBugTest.zip" rel="noreferrer">ShaderBugTest project</a>  To make the error reproducible, you need to have Microsoft's "Application Verifier" set to watch for Heap errors.  An enclosed Read Me has more detailed steps to reproduce.</p>