1

In SDL, lets say I'm rendering text to a surface. Then I blit it with the scene's main surface to form a composite image to flip.

I get the text surface into a class-level member called 'messageSurface':

messageSurface = TTF_RenderText_Solid( ..... );

So, do I have to call

SDL_FreeSurface(messageSurface);

on every loop to prevent memory leaks? Or will SDL "reuse" the old surface the next time around?

Mossen
  • 247
  • 1
  • 9
  • Whether it's class-level or not makes no difference. TTF_RenderText_Solid creates a new object and returns a pointer to it, and assignment in C++ overwrites the old value, so you no longer have a reference to the previous surface, meaning it will leak. – Kylotan Aug 17 '12 at 10:30

1 Answers1

1

According to documentation here:

http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf_43.html

the pointer returned is to a new surface. So yes, you need to free the surface to avoid memory leaks.

Offhand, that seems a rather odd API design. Instead, I would have expected you to pass an allocated surface to the function.

Jari Komppa
  • 7,873
  • 3
  • 30
  • 62