Freeing surfaces

Lyle Hanson wrote:

Hi!

Just wondering if it’s necessary to SDL_FreeSurface() all of my surfaces
at the end of the program before I quit. I’m developing under Linux,
which AFAIK won’t let me leak memory by not freeing surfaces when my
program ends. Plus I’d have to keep track of all the surfaces I load (no
big deal, but I’m still wondering…). Do I need to free them so Windows
(for example) will be able to reclaim the memory? Somebody give me some
advice here.

I don’t think any platform you need to worry about now-a-days will leak
memory (unless it’s system/shared memory, but even then, it shouldn’t be a
problem in most cases). However, there’s the serious issue of cleanliness.
It’s more or less a taboo of programming to not free memory you are using.
And it really isn’t an issue to keep track of surfaces. Something as simple
as a staticly allocated linked list, with a function “add_surface
(SDL_Surface *)” and another function “delete_all_surfaces (void)” should
suffice. It would be really trivial to even add a struct that stores a
unique ID for each surface (which could just be the pointer to memory, or
the name of the file the image was loaded from, in the case of images) and
use that to lookup surfaces on demand, for when you need to reload surfaces
(something you need to do in Windows, at least, and maybe X), and when you
need various parts of your program to utilize shared surfaces. You could
even go so far as to load images on demand. Use a function like
"get_surface (const char *id)" and it could load the surface from a file if
it’s not in the list.

Anyways, I’m getting way off track here - the short answer to your question
is “no”, the best answer is you “you should free surfaces”, and the long
answer it above.

Sean Etc.> Thanks,

Lyle

Hi!

Just wondering if it’s necessary to SDL_FreeSurface() all of my surfaces
at the end of the program before I quit. I’m developing under Linux,
which AFAIK won’t let me leak memory by not freeing surfaces when my
program ends. Plus I’d have to keep track of all the surfaces I load (no
big deal, but I’m still wondering…). Do I need to free them so Windows
(for example) will be able to reclaim the memory? Somebody give me some
advice here.

Thanks,
Lyle

If the surface is in VRAM on MacOS, you will lose that section of VRAM for
use by future programs (you have to reboot to get it back).> From: Lyle Hanson

Reply-To: sdl at lokigames.com
Date: Sun, 10 Dec 2000 20:13:27 -0500 (EST)
To: sdl at lokigames.com
Subject: [SDL] Freeing surfaces

Hi!

Just wondering if it’s necessary to SDL_FreeSurface() all of my surfaces
at the end of the program before I quit. I’m developing under Linux,
which AFAIK won’t let me leak memory by not freeing surfaces when my
program ends. Plus I’d have to keep track of all the surfaces I load (no
big deal, but I’m still wondering…). Do I need to free them so Windows
(for example) will be able to reclaim the memory? Somebody give me some
advice here.

Thanks,
Lyle

…Plus I’d have to keep track of all the surfaces I load (no
big deal, but I’m still wondering…). Do I need to free them so Windows
(for example) will be able to reclaim the memory? Somebody give me some
advice here.

Here’s an idea. You can have an array of SDL_image pointers (or just a
bunch of pointers), and make sure you initially set them all to null
(literally, 0). If you decide to free a surface before the end of the
program, be sure to free it and then set it to null. Then at the end of
your program, go through the list of pointers, and for pointer that isn’t
null, free it. No linked lists, no mem leaks, it’s all good!

-Brent

If the surface is in VRAM on MacOS, you will lose that section of VRAM for
use by future programs (you have to reboot to get it back).

Great, thanks. Exactly the kind of thing I was curious about.

LyleOn Sun, 10 Dec 2000, Darrell Walisser wrote: