Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

share|improve this question
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
up vote 1 down vote accepted

According to documentation here:

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.

share|improve this answer
It's impractical to know what size surface you'd have to pass in. – Kylotan Aug 17 '12 at 10:28
More impractical than reallocating bunch of surfaces each frame? I do get your point, though. I would have given several options. – Jari Komppa Aug 17 '12 at 12:21
If the text doesn't change, just use the same surface. – Kylotan Aug 17 '12 at 13:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.