Search code examples
c++memory-leakssdlsdl-2sdl-ttf

freeing an SDL_Surface correctly?


Can i free my SDL_Surface* surf by doing this:

    SDL_Surface* surf;
    for(int i = 0; i < 5; i++){
         surf = TTF_RenderText_Blended(foofont, foostring, foocolor);
    }
    SDL_FreeSurface(surf);
    surf = NULL;

Or does this cause memory leaks?


Solution

  • Edited: Based on how I understand the documentation, that will cause a leak. Seeing as you are basically calling the function 5 times, where it creates a new surface each time. Ie, you need to free all the surfaces each time. so

    for(int i = 0; i < 5; i++)
    {
        surf = TTF_RenderText_Blended(foofont, foostring, foocolor);
        SDL_FreSurface(surf)
    }
    surf = NULL;
    

    You can read the documentation here:

    https://www.libsdl.org/projects/docs/SDL_ttf/SDL_ttf_44.html

    https://wiki.libsdl.org/SDL_FreeSurface