glCreateShader makes program crash

Hello I have this code line:

GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);

that apparently causes my program to crash with a segmentation fault (I cannot print anything in the terminal after it)

The same happens if I swap the above line by GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

So, what can be causing it? An error of mine? (if yes, of what kind?). Perhaps some driver issue? How can I try to diagnose it ? Thanks for the input.

Is that the very first OpenGL call you make? If so, it’s likely that you’re calling it without a context being bound. If you’re using GLUT, glutCreateWindow must be called before calling any OpenGL functions. Other toolkits may limit the use of OpenGL functions to specific methods or callbacks.

Otherwise, I’m guessing that you’re using GLEW (or similar) and either it hasn’t been initialised or you only have the OpenGL 1.1 fallback software implementation available, so glCreateShader is a null pointer. In which case, glCreateShader is probably the first call to a function which isn’t present in OpenGL 1.1.

No, there is at least two calls, to glEnable and to glClearColor.

I make a call to glewInit before calling glCreateShader. It worked before when I was using fixed function OpenGL. So it’s likely the last case ? If so, is there a way to know by sure ?

Yes.

Print the result of glGetString with arguments of GL_VENDOR, GL_RENDERER and GL_VERSION. I suspect that the results will be “Microsoft Corporation”, “GDI Generic” and “1.1.0” respectively.

Fixing the issue typically involves reinstalling the drivers for the video hardware.

They are:

GL_VENDOR: “Tungsten Graphics, Inc”

GL_RENDERER: “Mesa DRI Intel® G41 GEM 20091221 2009Q4”

GL_VERSION: “2.1 Mesa 7.7.1”

glewInit() doesn’t create an OpenGL context.

What are you calling to create an OpenGL context before glewInit()?

Nothing. The first lines are:

glewInit ();
   /*if (!GLEW_VERSION_2_0) {
      cout << "glew not available. closing..." << endl;
      exit (-1);
   }*/
   
glfwInit ();
glEnable (GL_DEPTH_TEST);
if (!glfwOpenWindow (1024,768,8,8,8,0,8,0,GLFW_WINDOW)) {
    glfwTerminate();
     exit (1);
}

By the way, if I take the comments out of

if (!GLEW_VERSION_2_0) {
      cout << "glew not available. closing..." << endl;
      exit (-1);
   }

it says that glew is not available.

You need to call glfwInit before glewInit. glewInit requires an OpenGL context to be bound.

1 Like

Actually, you need to call glfwOpenWindow before glewInit.

1 Like