[SDL] Need help tracking down a SegFault

Michael Sullivan michael at espersunited.com
Sun Feb 24 08:54:00 PST 2008


I need help finding the source of a SegFault.  I believe that I have the
search narrowed down to a single line of code.  Here's the build and run
output:

michael at camille ourrpg $ make && ./main
g++ -W -Wall `sdl-config --cflags`  -c battle.cpp
battle.cpp: In member function 'bool Battle::initialize()':
battle.cpp:85: warning: name lookup of 'i' changed
battle.cpp:11: warning:   matches this 'i' under ISO standard rules
battle.cpp:58: warning:   matches this 'i' under old rules
battle.cpp: In member function 'SDL_Surface* Battle::drawString(int,
int, std::string)':
battle.cpp:109: warning: missing initializer for member
'SDL_Color::unused'
g++ -W -Wall `sdl-config --cflags`  -c character.cpp
g++ -W -Wall `sdl-config --cflags`  -c ally.cpp
g++ -W -Wall `sdl-config --cflags`  -c main.cpp
g++ -o main battle.o character.o ally.o main.o `sdl-config --libs`
-lSDL_image -lSDL_gfx -lSDL_ttf -lz 
Line is 65; i = 0
Segmentation fault
Here's the offending function:

    23	bool Battle::initialize()
    24	{
    25	   if (SDL_Init(SDL_INIT_VIDEO) != 0)
    26	   {
    27	      printf("Unable to initialize SDL:  %s\n", SDL_GetError());
    28	      return false;
    29	   }
    30	   atexit(SDL_Quit);
    31	   
    32	   if( TTF_Init() == -1 ) 
    33	   { 
    34	      printf("Could not intialize the font:  %s\n",
SDL_GetError());
    35	      return false;
    36	   }
    37	   font = TTF_OpenFont( "lazy.ttf", 18 );
       
    38	   if( font == NULL ) 
    39	   {
    40	      printf("Error loading font:  %s\n", SDL_GetError());
    41	      return false;
    42	   }
    43	   SDL_WM_SetCaption( "OurRPG - Battle Screen", NULL );
    44	   screen =  SDL_SetVideoMode(1000, 675, 16, SDL_DOUBLEBUF |
SDL_HWSURFACE);
    45	   if (screen == NULL)
    46	   {
    47	      printf("Unable to set video mode:  %s\n", SDL_GetError());
    48	      return false;
    49	   }
       
       
    50	   for (int i = 0; i < 4; i++)
    51	   {
    52	      status[i] = NULL;
    53	      party[i].setX(600);
    54	      party[i].setY(i * 100);
    55	      image[i] = party[i].getImage();
    56	if (image[i] == NULL) printf("It's NULL!!!\n");
    57	printf ("Line is %d; i = %i\n", __LINE__, i);
    58	      colorkey = SDL_MapRGB(image[i]->format, 0, 255, 0);
    59	printf ("Line is %d; i = %i\n", __LINE__, i);
    60	      SDL_SetColorKey(image[i], SDL_SRCCOLORKEY, colorkey);
    61	      
    62	printf ("Line is %d; i = %i\n", __LINE__, i);
    63	      src[i].x = 0;
    64	printf ("Line is %d; i = %i\n", __LINE__, i);
    65	      src[i].y = 0;
    66	printf ("Line is %d; i = %i\n", __LINE__, i);
    67	      src[i].w = image[i]->w;
    68	printf ("Line is %d; i = %i\n", __LINE__, i);
    69	      src[i].h = image[i]->h;
    70	printf ("Line is %d; i = %i\n", __LINE__, i);
    71	      
    72	      dest[i].x = 600;
    73	      dest[i].y = i * 100;
    74	      dest[i].w = src[i].w;
    75	      dest[i].h = src[i].h;
    76	   }
    77	printf ("Line is %d; i = %i\n", __LINE__, i);
       
    78	   drawLines();
    79	   drawStats();
       
    80	   return true;
       
    81	}

As you can see, the printf on 65 is firing, so image[i] is not NULL.
The problem seems to be with the colorkey line.  Is there anything wrong
with how I've written this line?  Please help!



More information about the SDL mailing list