SDL_SetVideoMode and SDL_FreeSurface best practices?

I like it when my app doesn’t leak memory. :stuck_out_tongue: So, I’m wondering, which of these
two code snippets is the best sequence of events?

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
Surface = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
// use surface here
SDL_Quit();
SDL_FreeSurface( Surface );

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
Surface = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
// use surface here
SDL_FreeSurface( Surface );
SDL_Quit();

In other words, is it better to free Surface before or after SDL_Quit? Or, does
SDL_Quit delete any surfaces created by SDL_SetVideoMode automatically?

Note that I’ve left out any error checking code to make the code samples simple.

Thanks,
Doug.

Hello !

This would be right :

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
Surface = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
// use surface here
SDL_Quit();

Both are wrong, SDL_Quit itself removes the Screen Surface.

CU

But will work SDL_FreeSurface( Surface ); to delete the current video
mode?..
Is there another way to unset that video mode? (another than
SDL_QuitSubsystem…);

CU

2006/8/15, Torsten Giebl :>

Hello !

This would be right :

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
Surface = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
// use surface here
SDL_Quit();

Both are wrong, SDL_Quit itself removes the Screen Surface.

CU


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


Phantom Lord
Caelis Studios —> From Gods Hands To Yours

Hello !

But will work SDL_FreeSurface( Surface ); to delete the current video
mode?.. Is there another way to unset that video mode? (another than
SDL_QuitSubsystem…);

I think no. SDL_QuitSubsystem is the safest way to do it.
Even better than that would be using SDL_Quit as on some systems
the Audiocontext maybe connected to the Window directly.

CU

What Torsten said is correct. Here’s a snipplet from the SDL wiki.

“The surface returned is freed by SDL_Quit and should not be freed by
the caller. Note: This rule includes consecutive calls to
SDL_SetVideoMode (i.e. resize or rez change) - the pre-existing surface
will be released automatically.”

Also, calling SDL_FreeSurface() after SDL_Quit() will probably crash
your program.