Jump to content
  • Advertisement
Sign in to follow this  
theSecondt

SDL_FreeSurface is crashing!

This topic is 4321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why does the code below crash the app? i thought that if you allocate, deallocate, allocate again, it should work, no?
SDL_Surface* LoadIMG(const std::string image_path)
{
  //The image that's loaded
  SDL_Surface *loadedImage=NULL;

  //The optimized image that will be used
  SDL_Surface *optimizedImage=NULL;

  //Load the image
  loadedImage = IMG_Load( image_path.c_str() );

  //Create an optimized image
  optimizedImage = SDL_DisplayFormat( loadedImage );

  //Map the color key - purple ftw!
  Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0x00, 0xFF );

  //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
  SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );

  //Free the old image
  SDL_FreeSurface( loadedImage );

  return optimizedImage;
}



  sprite= LoadIMG( "gfx/dot.BMP" );
  SDL_FreeSurface(sprite);
  //sprite= NULL; -doesn't matter anyway
  sprite= LoadIMG( "gfx/dot.BMP" );


thanks!

Share this post


Link to post
Share on other sites
Advertisement
SDL_Surface *IMG_Load(const char *file);
img_load and all the rest of the functions i use return SDL_Surface*.
but i do suspect that there is a problem in that(LoadIMG) function.

Share this post


Link to post
Share on other sites
Couple things:

1. What does the debugger tell you? It should say more than just "You crashed! :(" And what values does the debugger say that all the variables there are?
2. I believe that let_bound wanted you to check what the function returns at run time. It wouldn't compile if it didn't return SDL_Surface*. So instead, check whether it actually loads the image, or whether it just returns NULL (which means something like the file path is wrong)?
3. What line does it crash on?
4. Are you sure that the file is actually there? If you're running the program from inside your IDE, is the file in the working directory that you've got the IDE set to use?

Share this post


Link to post
Share on other sites
If you are running in Hardware mode, try software instead. ie pass SDL_SWSURFACE to SDL_SetVideoMode in place of SDL_HWSURFACE.

I had this problem with hardware mode before.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!