[SDL] Problens with free surface

Ulrich Eckhardt doomster at knuut.de
Fri Jul 13 03:04:35 PDT 2007


On Thursday 12 July 2007 15:36:54 Rúben Lício wrote:
> 	SDL_Surface* sfcResized = NULL;						// Surface for all images
> display. BUG for freesurface.
> 	SDL_Rect recResized = {80,25,640,480};				// Store location of image at
> album. SDL_Event evnt;										// Event managerment.
> 	long current = 0;
> 	char screenShotFile[200+1];
>
> 	// Get last valid file from path.
> 	for (current=1;; current++)
> 	{
> 		FILE *fp = NULL;
> 		memset(screenShotFile, 0, sizeof(screenShotFile));
> 		sprintf(screenShotFile, "screenshots/shot%06d.png", current);
>
> 		fp = fopen(screenShotFile, "r");
>
> 		// If cant open, we found last file.
> 		if (!fp)
> 			break;
>
> 		fclose(fp);
> 	}
> 	--current;

Just one note: you could use stat() to find out if the file exists and is a 
regular file, too. Further, two more notes on string handling:
1. The memset() call is superfluous, sprintf() correctly terminates the 
string.
2. Use snprintf() for safety. Even better, use a function
 char const* get_screenshot_path(unsigned index);
that either formats the path into a static buffer or invokes exit() on error, 
because you are using the same code below again. ;)

> 	// If we have a valid last file, display it on screen.
> 	if (current>0)
> 	{
> 		memset(screenShotFile, 0, sizeof(screenShotFile));
> 		sprintf(screenShotFile, "screenshots/shot%06d.png", screenShotFile);
>
> 		LoadImg(&sfcResized, screenShotFile);
>
> 		SDL_ResizeXY(sfcResized, 640, 480, 2);

I can't find this function anywhere in SDL, are you using the external 
SDL_Resize lib? If so, this function supposedly returns a new surface(!) 
which obviously needs to be used from then on and also freed. Digging into 
said library, it gets even worse: it seems like the input surface is 
destroyed(WTF?) so you must not use sfcResized after calling this function. 
This library is so broken, words fail me. Well, after all the behaviour is 
documented (read the headers!) but still.

David, in case you're reading this, consider changing this please! Also, you 
notice that you couldn't get this to compile as C code under VS2005. This is 
probably due to the fact that your code is not valid C89 code because it 
mixes declarations and code is some functions. If you supply the error 
message, I'm sure we can work out the reason.

> When i call SDL_FreeSurface(sfcResized); some times it broken beause
> dont have a valid surface. Anyone can help me to find why I dont have
> a valid surface?

No surprise, see above. If stretching is all you need, there used to be a 
function in SDL (SDL_StretchBlit or somesuch) which was half-official but at 
least does the job without any surprises like SDL_ResizeXY, I'd use that 
until the SDL_Resize lib has been changed. Alternatively, you could fix it 
yourself or work around it.

BTW: your code mostly looks fine, but it completely lacks checking of 
returnvalues and errorcodes, so it's possible that something in between is 
failing without you even noticing. Don't start adding error handling when 
code begins to fail!

Uli


More information about the SDL mailing list