glTexImage2D seg fault, a simple example

I have created a simple test program (not using SMPEG - I sure am glad
that the problem isn’t in there!) that shows the seg fault problem.
Interestingly, this program shows the problem even with a single texture
(ntex = 1). Now some kind soul will be easily able to show me the error
of my ways.

/* To test creating and deleting textures */

#include “SDL.h”
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <GL/gl.h>
#include <GL/glu.h>

int ntex = 1;
static GLuint* texture_ids = NULL;
GLubyte* frame = NULL;

void draw(void)
{
GLuint i;
GLenum glerr;
GLint movie_width=768;
GLint skip_rows=0;
GLint skip_pixels=0;

glLoadIdentity( );

for (i = 0; i < ntex; i++) {
glBindTexture( GL_TEXTURE_2D, texture_ids[i] );
glPixelStorei( GL_UNPACK_ROW_LENGTH, movie_width );
glPixelStorei( GL_UNPACK_SKIP_ROWS, skip_rows );
glPixelStorei( GL_UNPACK_SKIP_PIXELS, skip_pixels );

/*
 * Drawing goes here, using glTexSubImage2D().
 */
fprintf(stderr,"Now drawing frame\n");

glerr = glGetError();
if (glerr != GL_NO_ERROR)
  fprintf(stderr,"GL ERROR: %d  %s\n",glerr, gluErrorString(glerr));

}
}

int main(void)
{
int i, k;
GLubyte* pixels; /* initial black texels /
SDL_Surface
screen;

if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 ) {
fprintf( stderr, “Couldn’t initialize SDL\n” );
return 1;
}
screen = SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL|SDL_FULLSCREEN);
if ( !screen ) {
fprintf( stderr, “Couldn’t set 1024x768 GL video mode: %s\n”,
SDL_GetError());
return 1;
}

/* This is the loop for creating, using, and deleting textures */
for (k=0; k < 2; k++) {
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glEnable( GL_TEXTURE_2D );
glEnable( GL_DITHER );

if (texture_ids)
  free(texture_ids);
texture_ids = (GLuint*) malloc( sizeof( GLuint ) * ntex );
if ( !texture_ids ) {
  return 1;
}

glGenTextures( ntex, texture_ids );

for (i=0; i<ntex; i++) {
  fprintf(stderr,"# %d  texture_ids: %d\n",i,texture_ids[i]);
}

/* Specify the textures, initially all black */
for ( i = 0; i < ntex; i++ ) {

  pixels = (GLubyte*) malloc( 256*256 * 4 );
  memset( pixels, 0, 256*256 * 4 );
  if( !pixels ) {
    glDeleteTextures( ntex, texture_ids );
    return 1;
  }

  /* Do all of our useful binding. */
  glBindTexture( GL_TEXTURE_2D, texture_ids[i] );
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR

);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
);

  /* Specify our 256x256 black texture. */
  glTexImage2D( GL_TEXTURE_2D,
                0,
                GL_RGB,
                256,
                256,
                0,
                GL_RGBA,
                GL_UNSIGNED_BYTE,
                pixels );
  free( pixels );
}

draw();

glDeleteTextures(ntex, texture_ids);

}
return 0;
}

Hi,

does the same thing on my system.

It is crashing in glTexImage2D() during the second iteration.
If you comment out this line in draw() it doesn’t crash any more:

 glPixelStorei( GL_UNPACK_ROW_LENGTH, movie_width );

This is documented as affecting the operation of glTexImage2D() and on the
first pass it defaults to zero.

You could try reseting it to zero before begining the second iteration,

cheers,
John.> ----- Original Message -----

From: bogle@ihug.co.nz (Gib Bogle)
To:
Sent: Monday, August 05, 2002 1:39 AM
Subject: [SDL] glTexImage2D seg fault, a simple example

I have created a simple test program (not using SMPEG - I sure am glad
that the problem isn’t in there!) that shows the seg fault problem.
Interestingly, this program shows the problem even with a single texture
(ntex = 1). Now some kind soul will be easily able to show me the error
of my ways.

/* To test creating and deleting textures */

#include “SDL.h”
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <GL/gl.h>
#include <GL/glu.h>

int ntex = 1;
static GLuint* texture_ids = NULL;
GLubyte* frame = NULL;

void draw(void)
{
GLuint i;
GLenum glerr;
GLint movie_width=768;
GLint skip_rows=0;
GLint skip_pixels=0;

glLoadIdentity( );

for (i = 0; i < ntex; i++) {
glBindTexture( GL_TEXTURE_2D, texture_ids[i] );
glPixelStorei( GL_UNPACK_ROW_LENGTH, movie_width );
glPixelStorei( GL_UNPACK_SKIP_ROWS, skip_rows );
glPixelStorei( GL_UNPACK_SKIP_PIXELS, skip_pixels );

/*
 * Drawing goes here, using glTexSubImage2D().
 */
fprintf(stderr,"Now drawing frame\n");

glerr = glGetError();
if (glerr != GL_NO_ERROR)
  fprintf(stderr,"GL ERROR: %d  %s\n",glerr, gluErrorString(glerr));

}
}

int main(void)
{
int i, k;
GLubyte* pixels; /* initial black texels /
SDL_Surface
screen;

if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 ) {
fprintf( stderr, “Couldn’t initialize SDL\n” );
return 1;
}
screen = SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL|SDL_FULLSCREEN);
if ( !screen ) {
fprintf( stderr, “Couldn’t set 1024x768 GL video mode: %s\n”,
SDL_GetError());
return 1;
}

/* This is the loop for creating, using, and deleting textures */
for (k=0; k < 2; k++) {
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glEnable( GL_TEXTURE_2D );
glEnable( GL_DITHER );

if (texture_ids)
  free(texture_ids);
texture_ids = (GLuint*) malloc( sizeof( GLuint ) * ntex );
if ( !texture_ids ) {
  return 1;
}

glGenTextures( ntex, texture_ids );

for (i=0; i<ntex; i++) {
  fprintf(stderr,"# %d  texture_ids: %d\n",i,texture_ids[i]);
}

/* Specify the textures, initially all black */
for ( i = 0; i < ntex; i++ ) {

  pixels = (GLubyte*) malloc( 256*256 * 4 );
  memset( pixels, 0, 256*256 * 4 );
  if( !pixels ) {
    glDeleteTextures( ntex, texture_ids );
    return 1;
  }

  /* Do all of our useful binding. */
  glBindTexture( GL_TEXTURE_2D, texture_ids[i] );
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR

);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
);

  /* Specify our 256x256 black texture. */
  glTexImage2D( GL_TEXTURE_2D,
                0,
                GL_RGB,
                256,
                256,
                0,
                GL_RGBA,
                GL_UNSIGNED_BYTE,
                pixels );
  free( pixels );
}

draw();

glDeleteTextures(ntex, texture_ids);

}
return 0;
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl