Segmentation fault in SDL_DisplayFormat{,Alpha}

Hi,

I’m starting playing around with SDL. Im using Lazy FOo’
tutorials(http://lazyfoo.net/SDL_tutorials/index.php) to help me.

The prog here used to work, i don’t know what i did to make it crash now. No
changes were made in the crashing functions.
gdb reports:
#0 0xb7f0d0e3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0
#1 0x08049a74 in load_image (filename=@0xbfe0eb08) at wrappers.cpp:18
#2 0x08049495 in Plane (this=0xbfe0eb44, planeid=0, planetype=0)
at plane.cpp:22
#3 0x08049ccc in main () at abricots.cpp:46

And here’s my load_image function:

SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
std::cout << “Image loaded!\n”;
Uint32 colorkey = SDL_MapRGB( loadedImage->format, 0xFF, 0, 0xFF );
optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}

The image being loaded is a simple 24 bits png wich, as i said, loaded fine
before. SDL_SetVideoMode is called as SDL_SetVideoMode( 800, 600, 24,
SDL_HWSURFACE );

The last changes made inclued a timer to move the surface smoothly regardless
of the framerate. Let me know if you want me to join the whole prog (7
files).

Regards,

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

charly wrote:

And here’s my load_image function:

if( loadedImage != NULL )
{
    std::cout << "Image loaded!\n";
    Uint32 colorkey = SDL_MapRGB( loadedImage->format, 0xFF, 0, 0xFF );
    optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
    SDL_FreeSurface( loadedImage );
}
return optimizedImage;

}

Hi, I think you want to say
if( loadedImage == null)
{
error
}

So, the way you have it, every time it loads an image, it will return
an error.

Jim

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFG7G8dQuDJiZ/QrH0RAjBMAKDBb5gGRYZHHq7LRrCJQfjgQ/Ky8gCgxpxE
Hny/lND8/mE0tVmwiEVBHts=
=NXF9
-----END PGP SIGNATURE-----

when the image that you load doesn’t load correctly,you get a NULL pointer.but this function still returns optimizedImage pointer.
?2007-09-14?charly ???
Hi, I’m starting playing around with SDL. Im using Lazy FOo’ tutorials(http://lazyfoo.net/SDL_tutorials/index.php) to help me. The prog here used to work, i don’t know what i did to make it crash now. No changes were made in the crashing functions. gdb reports: #0 0xb7f0d0e3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0 #1 0x08049a74 in load_image (filename=@0xbfe0eb08) at wrappers.cpp:18 #2 0x08049495 in Plane (this=0xbfe0eb44, planeid=0, planetype=0) at plane.cpp:22 #3 0x08049ccc in main () at abricots.cpp:46 And here’s my load_image function: SDL_Surface *load_image(std::string filename) { SDL_Surface *loadedImage = NULL; SDL_Surface *optimizedImage = NULL; loadedImage = IMG_Load( filename.c_str() ); if( loadedImage != NULL ) { std::cout << “Image loaded!\n”; Uint32 colorkey = SDL_MapRGB( loadedImage->format, 0xFF, 0, 0xFF ); optimizedImage = SDL_DisplayFormatAlpha( loadedImage ); SDL_FreeSurface( loadedImage ); } return optimizedImage; } The image being loaded is a simple 24 bits png wich, as i said, loaded fine before. SDL_SetVideoMode is called as SDL_SetVideoMode( 800, 600, 24, SDL_HWSURFACE ); The last changes made inclued a timer to move the surface smoothly regardless of the framerate. Let me know if you want me to join the whole prog (7 files). Regards, _______________________________________________ SDL mailing list SDL at lists.libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

My fault. I moved the object creation before calling SDL_Init().

But thanks for pointing me changes needed in this function.

CharlyOn Friday 14 September 2007 13:13:43 you wrote:

Hi,

I’m starting playing around with SDL. Im using Lazy FOo’
tutorials(http://lazyfoo.net/SDL_tutorials/index.php) to help me.

The prog here used to work, i don’t know what i did to make it crash now.
No changes were made in the crashing functions.
gdb reports:
#0 0xb7f0d0e3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0
#1 0x08049a74 in load_image (filename=@0xbfe0eb08) at wrappers.cpp:18
#2 0x08049495 in Plane (this=0xbfe0eb44, planeid=0, planetype=0)
at plane.cpp:22
#3 0x08049ccc in main () at abricots.cpp:46

And here’s my load_image function:

SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
std::cout << “Image loaded!\n”;
Uint32 colorkey = SDL_MapRGB( loadedImage->format, 0xFF, 0, 0xFF );
optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}

The image being loaded is a simple 24 bits png wich, as i said, loaded fine
before. SDL_SetVideoMode is called as SDL_SetVideoMode( 800, 600, 24,
SDL_HWSURFACE );

The last changes made inclued a timer to move the surface smoothly
regardless of the framerate. Let me know if you want me to join the whole
prog (7 files).

Regards,