1
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\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
    Commented Aug 17, 2012 at 10:30

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ It's impractical to know what size surface you'd have to pass in. \$\endgroup\$
    – Kylotan
    Commented Aug 17, 2012 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\$ Commented Aug 17, 2012 at 12:21
  • \$\begingroup\$ If the text doesn't change, just use the same surface. \$\endgroup\$
    – Kylotan
    Commented Aug 17, 2012 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .