OpenGL: switching polygon mode causes segfault
<p>My computer runs Ubuntu 16.04 with an Intel graphics card. I am using Mesa 11.2 for my OpenGL profile.</p>

<p>My humble OpenGL program displays a simple square in a window. I wanted to make the program toggle in and out of wireframe mode if I pressed a certain key, so I defined the following callback function:</p>

<pre><code>void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mode) {
    if (key == GLFW_KEY_ESCAPE and action == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, GL_TRUE);
    }
    if (key == GLFW_KEY_M and action == GLFW_PRESS) {
        // Find the rasterizing mode.
        GLint rastMode;
        glGetIntegerv(GL_POLYGON_MODE, &amp;rastMode);

        // Switch modes depending on current rasterizing mode.
        if (rastMode == GL_FILL) {
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        }
        else {
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        }
    }
}
</code></pre>

<p>Unfortunately, pressing m while my program is running will cause a segfault. Curiously though, on my <em>other</em> computer (running Ubuntu 16.04 but with an Nvidia GPU) I have no such issue and the program works as expected.</p>

<p>The problem isn't with <code>glPolygonMode</code>: I can place that inside my <code>main</code> function and the program will successfully switch modes. The issues seems to lay with <code>glGetIntegerv</code>. If I call that function inside my <code>main</code> function (say, right outside the game loop), my square will refuse to appear (though there is not segfault).</p>

<p>Here's the complete code:</p>

<pre><code>#include &lt;array&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;GL/glew.h&gt;
#include &lt;GLFW/glfw3.h&gt;

// Vertex and fragment shader source files.
constexpr char VERTEX_SHADER_SOURCE_FILE[]   = "simple_vertex.shader";
constexpr char FRAGMENT_SHADER_SOURCE_FILE[] = "simple_fragment.shader";

// Window properties.
constexpr int  WINDOW_WIDTH   = 800;
constexpr int  WINDOW_HEIGHT  = 800;
constexpr char WINDOW_TITLE[] = "Triangle";

// Background colour.
constexpr std::array&lt;GLfloat, 4&gt; bgColour { 0.3f, 0.1f, 0.3f, 1.0f };

/*
 * Instructs GLFW to close window if escape key is pressed and to toggle between rasterizing modes
 * if m is pressed.
 */
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mode);

int main() {
    // Initialize GLFW.
    if (not glfwInit()) {
        std::cerr &lt;&lt; "ERROR: Failed to start GLFW.\n";
        return 1;
    }

    // Set required OpenGL version.
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Create a window object and bind it to the current context.
    GLFWwindow *window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, nullptr,
                                          nullptr);
    if (not window) {
        std::cerr &lt;&lt; "ERROR: Failed to create GLFW window.\n";
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent(window);

    // Set callback functions.
    glfwSetKeyCallback(window, keyCallback);

    // Initialize GLEW with experimental features enabled.
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        std::cerr &lt;&lt; "ERROR: Failed to start GLEW.\n";
        glfwTerminate();
        return 1;
    }

    // Display information on the current GL connection.
    std::cout &lt;&lt; "Renderer: " &lt;&lt; glGetString(GL_RENDERER) &lt;&lt; std::endl;
    std::cout &lt;&lt; "Version: " &lt;&lt; glGetString(GL_VERSION) &lt;&lt; std::endl;
    std::cout &lt;&lt; "Shading Language: " &lt;&lt; glGetString(GL_SHADING_LANGUAGE_VERSION) &lt;&lt; std::endl;

    // Define the viewport dimensions.
    int width, height;
    glfwGetFramebufferSize(window, &amp;width, &amp;height);
    glViewport(0, 0, static_cast&lt;GLsizei&gt;(width), static_cast&lt;GLsizei&gt;(height));

    // Create a vertex shader object.
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

    // Load the vertex shader source code.
    std::string vertexShaderSource;
    std::ifstream vsfs(VERTEX_SHADER_SOURCE_FILE);
    if (vsfs.is_open()) {
        std::stringstream ss;
        ss &lt;&lt; vsfs.rdbuf();
        vertexShaderSource = ss.str();
    }
    else {
        std::cerr &lt;&lt; "ERROR: File " &lt;&lt; VERTEX_SHADER_SOURCE_FILE &lt;&lt; " could not be found.\n";
        glfwTerminate();
        return 1;
    }

    // Attach the shader source code to the vertex shader object and compile.
    const char *vertexShaderSource_cstr = vertexShaderSource.c_str();
    glShaderSource(vertexShader, 1, &amp;vertexShaderSource_cstr, nullptr);
    glCompileShader(vertexShader);

    // Check if compilation was successful.
    GLint success;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &amp;success);
    if (not success) {
        std::cerr &lt;&lt; "ERROR: Vertex shader compilation failed.\n";
        glfwTerminate();
        return 1;
    }

    // Create a fragment shader object.
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    // Load the fragment shader source code.
    std::string fragmentShaderSource;
    std::ifstream fsfs(FRAGMENT_SHADER_SOURCE_FILE);
    if (fsfs.is_open()) {
        std::stringstream ss;
        ss &lt;&lt; fsfs.rdbuf();
        fragmentShaderSource = ss.str();
    }
    else {
        std::cerr &lt;&lt; "ERROR: File " &lt;&lt; FRAGMENT_SHADER_SOURCE_FILE &lt;&lt; " could not be found.\n";
        glfwTerminate();
        return 1;
    }

    // Attach the shader source code to the fragment shader object and compile.
    const char *fragmentShaderSource_cstr = fragmentShaderSource.c_str();
    glShaderSource(fragmentShader, 1, &amp;fragmentShaderSource_cstr, nullptr);
    glCompileShader(fragmentShader);

    // Check if compilation was successful.
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &amp;success);
    if (not success) {
        std::cerr &lt;&lt; "ERROR: Fragment shader compilation failed.\n";
        glfwTerminate();
        return 1;
    }

    // Link the vertex and fragment shaders into a shader program.
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);   

    // Check that shader program was successfully linked.
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &amp;success);
    if (not success) {
        std::cerr &lt;&lt; "ERROR: Shader program linking failed.\n";
        glfwTerminate();
        return 1;
    }

    // Delete shader objects.
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    // Coordinates of square's vertices.
    std::array&lt;GLfloat, 12&gt; vertices {
         0.5f,  0.5f,  0.0f,
         0.5f, -0.5f,  0.0f,
        -0.5f, -0.5f,  0.0f,
        -0.5f,  0.5f,  0.0f
    };

    // Indices to draw.
    std::array&lt;GLuint, 6&gt; indices {
        0, 1, 3,
        1, 2, 3
    };

    // Create a vertex array object.
    GLuint vao;
    glGenVertexArrays(1, &amp;vao);
    glBindVertexArray(vao);

    // Create a vertex buffer object.
    GLuint vbo;
    glGenBuffers(1, &amp;vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    // Create an element buffer object.
    GLuint ebo;
    glGenBuffers(1, &amp;ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

    // Pass vertex data into currently bound vertex buffer object.
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW);

    // Pass index data into currently bound element buffer object.
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices.data(), GL_STATIC_DRAW);

    // Create and enable a vertex attribute.
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), static_cast&lt;GLvoid*&gt;(0));
    glEnableVertexAttribArray(0);

    // It is good practice to unbind the vertex array object, vertex buffer object, and element
    // buffer object.
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    // Set background colour.
    glClearColor(bgColour[0], bgColour[1], bgColour[2], bgColour[3]);

    // Main loop.
    while (not glfwWindowShouldClose(window)) {
        // Clear the screen of colours and poll for events.
        glClear(GL_COLOR_BUFFER_BIT);
        glfwPollEvents();

        // Inform OpenGL to use the shader program created above.
        glUseProgram(shaderProgram);

        // Bind the vertex array object and element buffer object.
        glBindVertexArray(vao);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

        // Draw the triangle using glDrawElements. The first argument gives the OpenGL primitive to
        // render, the second argument gives the number of vertices to draw, the third gives type
        // used to represent an index, and finally the last argument gives a possible offset in the
        // EBO.
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, static_cast&lt;GLvoid*&gt;(0));

        // Unbind the vertex array object (good practice).
        glBindVertexArray(0);

        // Swap buffers.
        glfwSwapBuffers(window);
    }

    // Clean up.
    glDeleteVertexArrays(1, &amp;vao);
    glDeleteBuffers(1, &amp;vbo);
    glDeleteProgram(shaderProgram);
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mode) {
    if (key == GLFW_KEY_ESCAPE and action == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, GL_TRUE);
    }
    if (key == GLFW_KEY_M and action == GLFW_PRESS) {
        // Find the rasterizing mode.
        GLint rastMode;
        glGetIntegerv(GL_POLYGON_MODE, &amp;rastMode);

        // Switch modes depending on current rasterizing mode.
        if (rastMode == GL_FILL) {
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        }
        else {
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        }
    }
}
</code></pre>