[SDL] SDL_FreeSurface issue?

Armond armond.sarkisian at gmail.com
Sun Nov 12 11:35:44 PST 2006


Hi everyone,

I want to know one thing. If I create a surface, allocate it and then 
deallocate it and then once more allocate it again, would that cause a problem. 
I seem to have a problem with the examples below. I thought if you deallocate 
it, all should be well right?

////////////////////////////////////////
image = LoadIMG( "ball.bmp" );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( "ball.bmp" );
if( !image ) // throw error
////////////////////////////////////////

Same for music. I seem to have a problem with this code if I load it and unload 
it a few times in a row. The program halts and says "Not Responding." It just 
freezes.

////////////////////////////////////////
Mix_HaltMusic( );
Mix_FreeMusic( my_sdl.music ); 
my_sdl.music = Mix_LoadMUS( "music.wav" ); 
if( !my_sdl.music ) // throw error
if( Mix_PlayMusic( my_sdl.music, -1 ) == -1 ) // throw error 
////////////////////////////////////////

Also, is it a good idea to do this prior to freeing any surfaces?

if( image != NULL ){ SDL_FreeSurface( image ); }
if( music != NULL ){ Mix_FreeMusic( music ); }

Just in case they are NULL, we woulden't want to free something that is not 
there

Any help would be appreciated.

Thanks!





More information about the SDL mailing list