I'm confused, I commented these 2 lines out resulting in no memory leaks, and when I added them again it's leaking.

However I can't understand why it's actually leaking?

This is the 2 lines of code one of them or both are leaking :

m_surface = TTF_RenderText_Solid(m_font, m_text.c_str(), m_color_fg);
m_texture = SDL_CreateTextureFromSurface(renderer, m_surface);

And here is the declaration of the 2 variables :

SDL_Surface* m_surface = nullptr;
SDL_Texture* m_texture = nullptr;

I'm not sure what else is needed to figure out why it's leaking.

  • 3
    \$\begingroup\$ They both allocate some kind of memory, probably. If you don't do whatever the API requires you do to do clean up that memory, you'll leak it. So make sure you call the appropriate cleanup routines, and you call them in pairs (so that each 'create' is eventually matched with a 'destroy' or similar). \$\endgroup\$
    – user1430
    Mar 25, 2016 at 16:16
  • 2
    \$\begingroup\$ Both of these methods create surfaces. The documentation examples suggest you should use SDL_FreeSurface to free them. \$\endgroup\$
    – Vaillancourt
    Mar 25, 2016 at 16:17
  • \$\begingroup\$ @AlexandreVaillancourt But how do I access the surfaces that they create? \$\endgroup\$
    – BiiX
    Mar 25, 2016 at 16:18
  • 4
    \$\begingroup\$ Read the documentation. \$\endgroup\$
    – Vaillancourt
    Mar 25, 2016 at 16:20
  • 1
    \$\begingroup\$ @BiiX Forget about memory leaks... You will realize what it truly is yourself after you learn a few other things. \$\endgroup\$
    – MickLH
    Mar 25, 2016 at 21:31


Browse other questions tagged .