Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glGenTextures segmentation fault?

So here is the problem: When I call glGenTextures I get a segmentation fault.

I'm on linux, and here is the code I'm currently using to investigate this:

#include <iostream>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

int main(int argc, char *argv[])
{
    GLuint texture;
    glGenTextures( 1, &texture );
    return texture;
}

Seems simple enough, right? Well, I traced the segfault with GDB (the GNU debugging tool) and it occurs when I call glGenTextures(), in the file /usr/lib/mesa/libGL.so.1.

That's my video driver's openGL code...

Any ideas?

like image 519
EHC Avatar asked Nov 02 '12 12:11

EHC


People also ask

How do you fix a segmentation fault?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

Why am I getting a segmentation fault?

In practice, segfaults are almost always due to trying to read or write a non-existent array element, not properly defining a pointer before using it, or (in C programs) accidentally using a variable's value as an address (see the scanf example below).

What does segmentation fault mean in C++?

A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.

How is segmentation fault prevented?

Use a #define or the sizeof operator at all places where the array length is used. Improper handling of NULL terminated strings. Forgetting to allocate space for the terminating NULL character. Forgetting to set the terminating NULL character.


1 Answers

The code as shown hasn't done anything to set up a valid OpenGL context, so it can't use OpenGL.

How to do this varies by platform. If you don't want to dig down in your target platform's way of doing this, you can use something like GLFW to do it fairly portably, but of course that adds a dependency.

like image 95
unwind Avatar answered Sep 18 '22 14:09

unwind