How safe is not freeing a surface all the time?

How safe is calling SDL_CreateRGBSurfaceFrom many times on a certain
SDL_Surface* and then doing a sigle SDL_FreeSurface at the end?

How safe is calling SDL_CreateRGBSurfaceFrom many times on a certain
SDL_Surface* and then doing a sigle SDL_FreeSurface at the end?

SDL_CreateRGBSurfaceFrom() doesn’t increment the reference count of the
surface that you passed the pixels from, so once you free it, the surfaces
that were created in SDL_CreateRGBSurfaceFrom() are pointing at garbage.

So… if I understand you correctly… not safe at all? :slight_smile:

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

I used to loop this:

image = SDL_CreateRGBSurfaceFrom( … );
SDL_BlitSurface(image, &rect, screen, NULL);
SDL_UpdateRect(screen,0,0,0,0);
SDL_FreeSurface(image);

I want to know if looping this is safe:

image = SDL_CreateRGBSurfaceFrom( … );
SDL_BlitSurface(image, &rect, screen, NULL);
SDL_UpdateRect(screen,0,0,0,0);

And then calling at the end of my app a single:

SDL_FreeSurface(image);

Sam Lantinga escribi?:>> How safe is calling SDL_CreateRGBSurfaceFrom many times on a certain

SDL_Surface* and then doing a sigle SDL_FreeSurface at the end?

SDL_CreateRGBSurfaceFrom() doesn’t increment the reference count of the
surface that you passed the pixels from, so once you free it, the surfaces
that were created in SDL_CreateRGBSurfaceFrom() are pointing at garbage.

So… if I understand you correctly… not safe at all? :slight_smile:

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I want to know if looping this is safe:

image = SDL_CreateRGBSurfaceFrom( … );
SDL_BlitSurface(image, &rect, screen, NULL);
SDL_UpdateRect(screen,0,0,0,0);

And then calling at the end of my app a single:

SDL_FreeSurface(image);

No, you’ll have a memory leak of all but the last image if you do this.

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

Yea memory leaks are bad :stuck_out_tongue: I found this out the hard way. The best way I’ve
found to prevent creating them with images is make a function that frees
the old image and then loads and optimizes the new image. It works real good
if you use an array of images rather than single ones. I had some problems
when not using them in an array. Not quite sure why but maybe had to do with
arrays being pointers in disguise.On 6/8/07, Sam Lantinga wrote:

I want to know if looping this is safe:

image = SDL_CreateRGBSurfaceFrom( … );
SDL_BlitSurface(image, &rect, screen, NULL);
SDL_UpdateRect(screen,0,0,0,0);

And then calling at the end of my app a single:

SDL_FreeSurface(image);

No, you’ll have a memory leak of all but the last image if you do this.

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Dunno but arrays are not pointers!!!

Kurapix

2007/6/10, Jonathan Greig :>

Yea memory leaks are bad :stuck_out_tongue: I found this out the hard way. The best way
I’ve found to prevent creating them with images is make a function that
frees the old image and then loads and optimizes the new image. It works
real good if you use an array of images rather than single ones. I had some
problems when not using them in an array. Not quite sure why but maybe had
to do with arrays being pointers in disguise.

On 6/8/07, Sam Lantinga wrote:

I want to know if looping this is safe:

image = SDL_CreateRGBSurfaceFrom( … );
SDL_BlitSurface(image, &rect, screen, NULL);
SDL_UpdateRect(screen,0,0,0,0);

And then calling at the end of my app a single:

SDL_FreeSurface(image);

No, you’ll have a memory leak of all but the last image if you do this.

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org