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

kaiserjohan wrote on Tuesday, July 30, 2013:

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?

elmindreda wrote on Tuesday, July 30, 2013:

Are you re-initializing your extension loader?

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

It’s Markdown; just indent.

kaiserjohan wrote on Tuesday, July 30, 2013:

Extension loader?

elmindreda wrote on Tuesday, July 30, 2013:

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

kaiserjohan wrote on Tuesday, July 30, 2013:

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

elmindreda wrote on Tuesday, July 30, 2013:

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

kaiserjohan wrote on Tuesday, July 30, 2013:

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)

elmindreda wrote on Tuesday, July 30, 2013:

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

kaiserjohan wrote on Tuesday, July 30, 2013:

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);?

elmindreda wrote on Wednesday, July 31, 2013:

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.

kaiserjohan wrote on Wednesday, July 31, 2013:

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.