SDL Memory Management

I’ve got an SDL project that uses strict SDL and ANSI C, and for some
reason it eats up ram. About 1.5% every top update on my computer – but
this doesn’t seem to make sense as nothing new is being loaded. If
anybody has the time, the code can be found on the CVS at
sf.net/projects/epiar. Compiling this under Win32 produces the same
results – slowly eating ram. Anybody have any ideas as to why it does
this? I could really use the help.

Does SDL-1.2.3 have a bug where it eats up ram every time you blit a
surface or something? I don’t think it does … any thoughts?–

I’ve never experienced memory leaking ever using SDL.

I got your code and debugged it a little. Your DrawText() function seems to
be the cause of your memory leaks. You call TTF_RenderUTF8_Solid() to get a
surface, but you never free that surface after you are done using it. So
it’s allocating more and more memory every frame. I put in a
SDL_FreeSurface(text) and now the memory usage is staying constant while
it’s running.

-Jason> ----- Original Message -----

From: chris@luethy.net (Christopher Thielen)
To:
Sent: Sunday, March 17, 2002 5:18 AM
Subject: [SDL] SDL Memory Management

I’ve got an SDL project that uses strict SDL and ANSI C, and for some
reason it eats up ram. About 1.5% every top update on my computer – but
this doesn’t seem to make sense as nothing new is being loaded. If
anybody has the time, the code can be found on the CVS at
sf.net/projects/epiar. Compiling this under Win32 produces the same
results – slowly eating ram. Anybody have any ideas as to why it does
this? I could really use the help.

Does SDL-1.2.3 have a bug where it eats up ram every time you blit a
surface or something? I don’t think it does … any thoughts?


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

Christopher thorough wrote:

I’ve got an SDL project that uses strict SDL and ANSI C, and for some
reason it eats up ram. About 1.5% every top update on my computer – but
this doesn’t seem to make sense as nothing new is being loaded. If
anybody has the time, the code can be found on the CVS at
sf.net/projects/epiar. Compiling this under Win32 produces the same
results – slowly eating ram. Anybody have any ideas as to why it does
this? I could really use the help.

Does SDL-1.2.3 have a bug where it eats up ram every time you blit a
surface or something? I don’t think it does … any thoughts?

I looked at your code…small enough to just look at…which makes it easy to find your error, I think.

If I’m not mistaken you run the menu multiple times, each time the menu function calls load_menu which loads a background image.
then the menu never frees it, but rather loads the background into a new surface every time the menu is called upon.

The same problem happens with the game function and load_sprites. Every time the game function is called, load_sprites loads all the sprites into new surfaces.
the only thing that is correct in there is the freeing of the temporary surface used to load into and SDL_DisplayFormat from, into a new surface.
the sprites therefore are loaded into new surfaces, as well as the background images for the game and menu functions, every time they are called, and never freed,
except when the program exits, then you depend on SDL and the OS to free the RAM you ate again and again.

Memory mismanagement is a common mistake…I’m surprised you didn’t check for it this thoroughly.

one solution is the load all the images once at the start, and then just use them from then on…and free then at program end.
another solution is to free the loaded sprites and backgrounds at the end of the menu and game functions.

good luck with the game though, we always like more games :)–
-==-
Jon Atkins
http://jcatki.2y.net/

Christopher Thielen wrote:

I’ve got an SDL project that uses strict SDL and ANSI C, and for some
reason it eats up ram. About 1.5% every top update on my computer – but
this doesn’t seem to make sense as nothing new is being loaded. If
anybody has the time, the code can be found on the CVS at
sf.net/projects/epiar. Compiling this under Win32 produces the same
results – slowly eating ram. Anybody have any ideas as to why it does
this? I could really use the help.

Does SDL-1.2.3 have a bug where it eats up ram every time you blit a
surface or something? I don’t think it does … any thoughts?

I apparently was looking at the wrong directory in the CVS…
the devel version still looks to do the same thing but in the init_sprites and game_main functions.
all the rest still seems true though, sprites and backgrounds are loaded, but rarely freed.
I didn’t check through much of the code, but I suspect that these errors could be in the display_instructions code as well.–
-==-
Jon Atkins
http://jcatki.2y.net/

Yeah, that was the major memory leak I discovered last night as well.
Thanks for pointing that out, and thanks to everybody else about the
menu/game loop not cleaning up. I’ll work on that, thanks.On Sun, 2002-03-17 at 04:24, Jason Hoffoss wrote:

I’ve never experienced memory leaking ever using SDL.

I got your code and debugged it a little. Your DrawText() function seems to
be the cause of your memory leaks. You call TTF_RenderUTF8_Solid() to get a
surface, but you never free that surface after you are done using it. So
it’s allocating more and more memory every frame. I put in a
SDL_FreeSurface(text) and now the memory usage is staying constant while
it’s running.

-Jason

----- Original Message -----
From: “Christopher Thielen” <@Christopher_Thielen>
To:
Sent: Sunday, March 17, 2002 5:18 AM
Subject: [SDL] SDL Memory Management

I’ve got an SDL project that uses strict SDL and ANSI C, and for some
reason it eats up ram. About 1.5% every top update on my computer – but
this doesn’t seem to make sense as nothing new is being loaded. If
anybody has the time, the code can be found on the CVS at
sf.net/projects/epiar. Compiling this under Win32 produces the same
results – slowly eating ram. Anybody have any ideas as to why it does
this? I could really use the help.

Does SDL-1.2.3 have a bug where it eats up ram every time you blit a
surface or something? I don’t think it does … any thoughts?


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


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