TTF_RenderText functions increasing memory usage

Hi. I’m writing a base engine class in SDL, using the TTF and mixer libraries.
I’m just about done - there are a few bugs but overall it’s ready to be used.

One of the bugs deals with the TTF_RenderText functions - whenever they’re run,
they increase the amount of RAM that my program is using. This is a problem, as
I’ve sat there and watched my program go from about 8.5 megs of RAM to 50MB in
about 2 minutes. I KNOW it’s these functions because if I comment them out and
run the program, the RAM usage stays at a steady 8.5MB

My questions are:

  1. Has anyone else had this problem, and if so, how did you fix it?
  2. Do you have any suggestions as to what might be causing this?

thanks.

Hi. I’m writing a base engine class in SDL, using the TTF and mixer libraries.
I’m just about done - there are a few bugs but overall it’s ready to be used.

One of the bugs deals with the TTF_RenderText functions - whenever they’re run,
they increase the amount of RAM that my program is using. This is a problem, as
I’ve sat there and watched my program go from about 8.5 megs of RAM to 50MB in
about 2 minutes. I KNOW it’s these functions because if I comment them out and
run the program, the RAM usage stays at a steady 8.5MB

My questions are:

  1. Has anyone else had this problem, and if so, how did you fix it?
  2. Do you have any suggestions as to what might be causing this?

You need to free the surface returned from TTF_RenderText() with
SDL_FreeSurface(). Are you doing that?

– JoshOn 11/4/05, Nick Stovall wrote:

thanks.


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

I would hazard a guess that you are simply not freeing whatever surfaces are
being returned by the rendering functions. If all you do is keep creating
new surfaces everytime you want text, you can eat up lots of memory quickly.
So, if you’re done with it, get rid of it.
-Dave
p.s. if this is not the case, posting a brief synopsis of your code might be
helpful.> ----- Original Message -----

From: nsto135@aol.com (Nick Stovall)
To:
Sent: Friday, November 04, 2005 10:31 PM
Subject: [SDL] TTF_RenderText functions increasing memory usage

Hi. I’m writing a base engine class in SDL, using the TTF and mixer
libraries.
I’m just about done - there are a few bugs but overall it’s ready to be
used.

One of the bugs deals with the TTF_RenderText functions - whenever they’re
run,
they increase the amount of RAM that my program is using. This is a
problem, as
I’ve sat there and watched my program go from about 8.5 megs of RAM to
50MB in
about 2 minutes. I KNOW it’s these functions because if I comment them out
and
run the program, the RAM usage stays at a steady 8.5MB

My questions are:

  1. Has anyone else had this problem, and if so, how did you fix it?
  2. Do you have any suggestions as to what might be causing this?

thanks.


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

As the other people have suggested, you’re probably not freeing the surface. I
had the same problem with my game, and it caused a massive memory leak that
would lock it up after 2 minutes.

Here’s a quick example:

textSur=TTF_RenderText_Solid(text,strOut,fontcolor); //set the text surface
SDL_BlitSurface(textSur,NULL,screen,&r_text); //blit text
SDL_Flip(screen);
SDL_FreeSurface(textSur);

Note: You’ve only got to free the surface if it’s something that’s going to be
constantly changing.

An example would be if you’d like to have text that displays your current score.
As your score changes, you’re gonig to have to set textSur to different values,
so you have to be sure the free the surface every time.

If you were using it to always display the same unchanging text, for example “-
PAUSE -”, you don’t have to clear it after every screen blit, but when you’re
done with the surface completely.

I hope I was clear with my answer.

Thanks for all the help. I thought you only had to free surfaces after you were
finished using them. That explains a lot, though… when the player is at the
main menu, the menu text was loaded into the cooresponding surface once per
frame, changing color depending on whether the mouse was over it or not. I had
about 5 images being loaded into 5 surfaces once per frame and I wasn’t freeing
them after each use. It never crashed, but it wasn’t long until it was using
300+ megs of RAM.

Thanks again for all the help you guys.

Nick Stovall <nsto135 aol.com> writes:

Thanks for all the help. I thought you only had to free surfaces after you were
finished using them. That explains a lot, though… when the player is at the
main menu, the menu text was loaded into the cooresponding surface once per
frame, changing color depending on whether the mouse was over it or not. I had
about 5 images being loaded into 5 surfaces once per frame and I wasn’t freeing
them after each use. It never crashed, but it wasn’t long until it was using
300+ megs of RAM.

Thanks again for all the help you guys.

I use SDL when programming for Xbox, so it locks the system up poretty quickly
when you’ve only got 64MB of RAM and you’re refreshing your score incorrectly on
every drame :\