title: Sharing textures between OpenGL contexts with GLFW3 is not working
body: <p>The program below is supposed to:</p>
  
  <ol>
  <li>Create an invisible <code>GLFWwindow* masterWindow</code></li>
  <li>Load a texture from a file, while the active OpenGL context is the one associated with <code>masterWindow</code> </li>
  <li>create a <code>GLFWwindow* childWindow</code>, with sharing activated between this window's OpenGL context and <code>masterWindow</code>'s context</li>
  <li>display a full-window quad with the texture in <code>childWindow</code></li>
  </ol>
  
  <p>However, this does not work, i.e. instead of the texture, I get random snippets of graphics memory displayed in <code>childWindow</code>. Displaying the texture does work if I make the context of <code>childWindow</code> current before loading it, which is why I think that the problem is neither with my shaders nor with my texture loading routine (which I took from here, <a href="https://github.com/DavidEGrayson/ahrs-visualizer/blob/master/png_texture.cpp" rel="nofollow noreferrer">https://github.com/DavidEGrayson/ahrs-visualizer/blob/master/png_texture.cpp</a>).
  I understand that I should be able to share the texture (see answer to this SO question: <a href="https://stackoverflow.com/questions/13581303/opengl-share-existing-textures-with-future-contexts">OpenGL - Share existing textures with future contexts?</a>), so what am I doing wrong? In case it matters, I use a late 2008 Macbook with a NVIDIA 9400m and Mavericks/OpenGL 3.3 installed.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #define GLEW_STATIC
  #include &lt;glew.h&gt;
  #include &lt;glfw3.h&gt;
  
  const char* vShader =
    "#version 150 core\n"
    "in vec2 vertex;"
    "in vec2 vertexUV;"
    "out vec2 UV;"
    "void main() {gl_Position = vec4(vertex,0,1);UV=vertexUV;}";
  const char* fShader =
   "#version 150 core\n"
   "uniform sampler2D sampler;"
   "in vec2 UV;"
   "out vec4 color;"
   "void main() {color = texture(sampler, UV);}";
  
  GLuint png_texture_load(const char * file_name, unsigned int * width=NULL, unsigned int * height=NULL);
  GLuint make_program(const char* vShader, const char* fShader);
  
  GLFWwindow* masterWindow(NULL);
  
  void create_window_with_texture(GLuint texture)
  {
    GLFWwindow* childWindow(glfwCreateWindow(256,256, "", NULL, masterWindow));
    glfwMakeContextCurrent(childWindow);
  
    // texture appears if loaded from here
    // texture=png_texture_load("rose.png");                                                                                                               
  
    GLfloat vertices[]={-1,-1,1,-1,-1,1,1,1, // window corners in clip space                                                                               
                    0,0,1,0,0,1,1,1};    // corners in uv-space                                                                                        
    GLubyte indices[]={0,2,1,3};             // points for a two triangle strip                                                                            
  
    GLuint vArray,vBuffer,iBuffer;
    glGenVertexArrays(1,&amp;vArray);glBindVertexArray(vArray);
    glGenBuffers(1,&amp;vBuffer);glBindBuffer(GL_ARRAY_BUFFER,vBuffer);
    glGenBuffers(1,&amp;iBuffer);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,iBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_DYNAMIC_DRAW);
  
    GLuint program(make_program(vShader,fShader));
    glUseProgram(program);
    glUniform1i(glGetUniformLocation(program,"sampler"),0);
    GLuint vertexPtr(GLuint(glGetAttribLocation(program,"vertex")));
    glEnableVertexAttribArray(vertexPtr);
    GLuint vertexUVPtr(GLuint(glGetAttribLocation(program, "vertexUV")));
    glEnableVertexAttribArray(vertexUVPtr);
    glVertexAttribPointer(vertexPtr, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0 );
    glVertexAttribPointer(vertexUVPtr, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)32);
  
    glActiveTexture(GL_TEXTURE0);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);
  
    glBindTexture(GL_TEXTURE_2D,texture);
    glDrawElements(GL_TRIANGLE_STRIP,4, GL_UNSIGNED_BYTE, (GLvoid*)0);
  
    glfwSwapBuffers(childWindow);
  }
  
  int main()
  {
    if (!glfwInit())
      return -1;
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_VISIBLE,GL_FALSE);
    masterWindow=glfwCreateWindow(10,10, "", NULL,NULL);
    glfwWindowHint(GLFW_VISIBLE,GL_TRUE);
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwMakeContextCurrent(masterWindow);
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if(GLEW_OK!=err)
      return -1;
    glGetError(); // catch benign glewInit() invalid enum error                                                                                            
  
    GLuint texture(0);
    texture=png_texture_load("rose.png");
  
    create_window_with_texture(texture);
  
    std::cin.get();
  
    return 0;
  }
  </code></pre>