Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Could glGetString(GL_VERSION) Be Causing a Seg Fault?

Tags:

c

opengl

I'm learning OpenGL on Fedora 13 and noticed that a call to glGetString is causing a seg fault. I've scraped Google, but come up with no solutions.

The code is simple:

#include <GL/gl.h>

int main() {
    glGetString(GL_VERSION);
    return 0;
}

Compile Command:

g++ -lGL main.cpp -o test.bin

Run result:

$ ./test.bin 
Segmentation fault (core dumped)

OpenGL Info:

$ glxinfo | grep OpenGL
OpenGL vendor string: Tungsten Graphics, Inc
OpenGL renderer string: Mesa DRI Intel(R) IGDNG_M GEM 20100328 2010Q1 
OpenGL version string: 2.1 Mesa 7.8.1
OpenGL shading language version string: 1.20
OpenGL extensions:

Any ideas or references are greatly appreciated.

Solution:

#include <iostream>
#include <GL/freeglut.h>

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutCreateWindow("test");
    glutFullScreen();
    std::cout << glGetString(GL_VERSION) << std::endl;
    return 0;
}
like image 777
Cryo Avatar asked Jun 09 '11 06:06

Cryo


People also ask

What causes seg fault?

A segfault occurs when a reference to a variable falls outside the segment where that variable resides, or when a write is attempted to a location that is in a read-only segment.

How can segmentation fault be avoided?

Regarding Best practices to avoid segmentation faults, testing the code with tools like Valgrind/Efence helps in catching memory over runs. Besides using tools, writing and organising code carefully helps to great extent.

What does segmentation fault 11 mean?

ANSWER. Signal 11, or officially know as "segmentation fault", means that the program accessed a memory location that was not assigned. That's usually a bug in the program. So if you're writing your own program, that's the most likely cause.


1 Answers

I doubt you can call even something as simple as glGetString without an OpenGL context.

like image 71
Nicolas Lefebvre Avatar answered Sep 18 '22 08:09

Nicolas Lefebvre