Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I keep on getting segmentation fault each time I load fourth texture - what type of texture, I mean filename, does not matter. I checked value of GL_TEXTURES_STACK_SIZE which turned out to be 10 so quite more than 4, isn't it?

Here're code fragments:

funciton to load texture from png

static GLuint gl_loadTexture(const char filename[]) {
    static int iTexNum = 1; 
    GLuint texture = 0;
    img_s *img = NULL;

    img = img_loadPNG(filename);
    if (img) {
        glGenTextures(iTexNum++, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexImage2D(GL_TEXTURE_2D, 0, img->iGlFormat, img->uiWidth, img->uiHeight, 0, img->iGlFormat, GL_UNSIGNED_BYTE, img->p_ubaData);
        img_free(img); //it may cause errors on windows
    } else printf("Error: loading texture '%s' failed!\n", filename);

    return texture;

actual loading

static GLuint textures[4];

static void gl_init() {
    (...) //setting up OpenGL

    /* loading textures */
    textures[0] = gl_loadTexture("images/background.png");
    textures[1] = gl_loadTexture("images/spaceship.png");
    textures[2] = gl_loadTexture("images/asteroid.png");
    textures[3] = gl_loadTexture("images/asteroid2.png"); //this is causing SegFault no matter which file I load!

Any ideas?

Problem is present on both Linux and Windows.

share|improve this question
Maybe a stupid guess : are you sure that the file images/asteroid2.png exists ? Can you try to compile in Debug Mode (adding the -g3 flag in the CFLAGS under Linux) and tell us where exactly it stops ? I'd suggest you to use Valgrind if you still can't find out what's going on. – Axel Isouard Sep 26 '12 at 18:42
up vote 2 down vote accepted

I am not sure, but change this:

glGenTextures(iTexNum++, &texture);

into this:

glGenTextures(1, &texture);

Here is written:

void glGenTextures( GLsizei n, GLuint* textures );

n - Specifies the number of texture names to be generated.

textures - Specifies an array in which the generated texture names are stored.

You are generating 1,2,3,4,... texture names instead of one, so glGenTextures() is trying to write to array ( texture[0] - texture[3] ), but you are passing single element GLuint texture = 0;, so you get segmentation fault.

And, please delete this double post.

share|improve this answer
great answer and stupid me :) – Robin92 Sep 9 '12 at 18:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.