Creating a new window and re-using the old context causes crash

Using GLFW
JohanJ
2013-07-30
2013-07-31
  • JohanJ
    JohanJ
    2013-07-30

    I am using GLFW 3.0.2 and when I want to switch the application to fullscreen, I create a new window and intend to use the OpenGL context of the old window so I don't have to reload all the meshes, materials, etc and can just draw it again...

    This is what I do when I want to create a new window, such as when first creating the window or switching fullscreen mode:

    bool GLFWWindowManager::SetupWindow(const uint32_t width, const uint32_t height)
        {
            glfwWindowHint(GLFW_SAMPLES, mMSAA);
            auto newWindow = glfwCreateWindow(width, height, mWindowTitle.c_str(), mFullscreen ? glfwGetPrimaryMonitor() : NULL, mWindow);   // mWindow is NULL first time
    
            if (!newWindow) 
                return false;
    
            if (mWindow)  // if old window present, destroy the old window
                glfwDestroyWindow(mWindow);
    
            glfwMakeContextCurrent(newWindow);
            mWindow = newWindow;
    
            glewExperimental = GL_TRUE;
            GLenum glewErr = glewInit();
            const GLubyte* l = glewGetErrorString(glewErr);
    
            glViewport(0, 0, (GLsizei) width, (GLsizei)height);
    
            return true;
        }
    
    // the flow:
    void GLFWWindowManager::Test()
    {
        SetupWindow(800, 600);
        glfwMakeContextCurrent(mWindow);
    
        // draw meshes
    
        mFullscreen = true;
        SetupWindow(800, 600);
    
        // try to draw some meshes, crashes on GLDrawElements
    }
    

    What am I doing wrong?

    also - how on earth to you encase something in code blocks here?

     
    Last edit: JohanJ 2013-07-30
  • Camilla Löwy
    Camilla Löwy
    2013-07-30

    Are you re-initializing your extension loader?

    also - how on earth to you encase something in code blocks here?

    It's Markdown; just indent.

     
  • JohanJ
    JohanJ
    2013-07-30

    Extension loader?

     
  • Camilla Löwy
    Camilla Löwy
    2013-07-30

    You're not using a library like GLEW or gl3w?

     
  • JohanJ
    JohanJ
    2013-07-30

    ohh yes, I am using GLEW along with it. No, I am not doing any other re-initialization with GLEW

     
  • Camilla Löwy
    Camilla Löwy
    2013-07-30

    You may need to reinitialize GLEW before using the new context.

     
  • JohanJ
    JohanJ
    2013-07-30

    Alright, tried that (edited first post) and glewInit() passes without error both times, still crashes on glDrawElements - what else could be the problem?

    (note: glewInit() did throw an error when I tried to do it before glfwMakeContextCurrent(), but glewInit() runs fine with what I posted)

     
    Last edit: JohanJ 2013-07-30
  • Camilla Löwy
    Camilla Löwy
    2013-07-30

    You may not be re-initializing all the relevant OpenGL state.

     
  • JohanJ
    JohanJ
    2013-07-30

    Ah, you have to set ALL the state again? I thought that would be carried over, as it is the same context?

    I suppose the OpenGL objects persist though? so I dont have to recreate all the buffer objects; but I have to set all their state again (GL_ARRAY_BUFFER, VAO's, etc)?

    What about states set on object, like

    glSamplerParameteri(mTextureSampler, GL_TEXTURE_WRAP_S, GL_REPEAT);?
    
     
    Last edit: JohanJ 2013-07-30
  • Camilla Löwy
    Camilla Löwy
    2013-07-31

    State on shared objects should be shared, yes, but all other state needs to be set for each context.

    I don't know if sampler objects are shared. You'd need to check the spec.

     
  • JohanJ
    JohanJ
    2013-07-31

    Alright yes that sounds like a plausible reason, I will be hacking around some see if I can fix it. Big thanks for help, hopefully this will fix it.