Segfault when creating a window

Hello, When attemping to create a window my program segfaults.
it segfaults on the glfwCreateWindow() function.
code:

#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"   
using namespace std;

void error_callback( int error, const char* description );
static void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods )

int main()
{

    if( !glfwInit() )
    {
        exit( EXIT_FAILURE );
    }
    //Set function that gets called when there is an error
    glfwSetErrorCallback( error_callback );
    //Create a window
    //set openGL version
    //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    GLFWwindow* window = glfwCreateWindow( 640, 480, "Title", NULL, NULL );

    //Checks if window was aculally created
    if( !window )
    {
        glfwTerminate();
        exit( EXIT_FAILURE );
    }

    glfwSetKeyCallback( window, key_callback );

    //set window as the active window
    glfwMakeContextCurrent( window );

    //load the right openGL context
    gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress );

    while( !glfwWindowShouldClose( window ) )
    {

    }

    //cleanup
    glfwDestroyWindow( window );
    glfwTerminate();
    return 0;
}

//error handling function
void error_callback( int error, const char* description )
{
    fprintf( stderr, "Error: %s\n", description );
}

void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods )
{
    if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS )
    {
        glfwSetWindowShouldClose( window, GLFW_TRUE );
    }
}

the command used to compile is;

g++ -Iinclude -o bin/game.o -ldl -lglfw -lGL ./src/main.cpp ./src/glad.c

I fixed the formatting in your post, but there was still a missing ; after the key_callback declaration which might have been lost in the copy/paste.

I made a minor alteration to use the full main function arguments to remove some warnings. After copying that I added a small amount of code int the while( !glfwWindowShouldClose( window ) ) loop to ensure that the program could capture events. This worked for me.

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

using namespace std;

void error_callback( int error, const char* description );
static void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods );

int main(int argc, char** argv)
{

    if( !glfwInit() )
    {
        exit( EXIT_FAILURE );
    }
    //Set function that gets called when there is an error
    glfwSetErrorCallback( error_callback );
    //Create a window
    //set openGL version
    //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    GLFWwindow* window = glfwCreateWindow( 640, 480, "Title", NULL, NULL );

    //Checks if window was aculally created
    if( !window )
    {
        glfwTerminate();
        exit( EXIT_FAILURE );
    }

    glfwSetKeyCallback( window, key_callback );

    //set window as the active window
    glfwMakeContextCurrent( window );

    //load the right openGL context
    gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress );

    while( !glfwWindowShouldClose( window ) )
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    //cleanup
    glfwDestroyWindow( window );
    glfwTerminate();
    return 0;
}

//error handling function
void error_callback( int error, const char* description )
{
    fprintf( stderr, "Error: %s\n", description );
}

void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods )
{
    if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS )
    {
        glfwSetWindowShouldClose( window, GLFW_TRUE );
    }
}
2 Likes

Thanks for you response. Sorry about the formatting, I was having trouble. I added the changes to the main loop, but the program still segfaults. using print statements I determined that the segfault occurs on the glfwCreateWindow() function call, nothing runs after that. I am running on arch linux and I installed GLFW from the source.

If you installed GLFW from source did you try compiling and running any of the example or tests, and did these work?

If not can you run any OpenGL applications at all? If you’re running GLX then can you run glxinfo to see what OpenGL capabilities you have, for example run glxinfo|grep OpenGL.

For your own code, can you run the program using gdb to find out where it’s crashing?

1 Like

Sorry for wasting your time, It appears to be a driver issue. None of the examples worked and when I tried to launch steam to test other OpenGL Applications that segfaulted too. Doing a system update and reboot solved the issue.

No Problem, glad you’ve resolved the issue.