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?

  • \$\begingroup\$ 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. \$\endgroup\$ – Kylotan Aug 17 '12 at 10:30

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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