This is the way I render the score in my main game , and I made this new project to understand how to use it... It works , the score is updated and it display on the screen but the problem is memory... it increase forever , from 20mb when I start untill it runs out of memory..it nevers stop. And in the main game it's working at 2fps... I hate TTF I just can't understand how it works and how should I do it... every tutorial is in c++ and I have to make the game in pure C for college...Help.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>

int main(int argc, char ** argv)
    bool quit = false;
    SDL_Event event;


    SDL_Window * window = SDL_CreateWindow("Example",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,480, 0);
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Surface * surface;
    SDL_Texture * texture;

    int score = 0;
    char buffer[50];
    int texW = 0;
    int texH = 0;
    TTF_Font * font = TTF_OpenFont("font3.ttf", 25);
    SDL_Color color = { 255, 255, 255 };

    while (!quit)


        switch (event.type)
        case SDL_QUIT:
            quit = true;

        score += 100;
        sprintf(buffer, "SCORE: %d", score);

        surface = TTF_RenderText_Solid(font, buffer, color);
        texture = SDL_CreateTextureFromSurface(renderer, surface);
        SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);

        SDL_Rect dstrect = { 0, 0, texW,texH };
        SDL_RenderCopy(renderer, texture, NULL, &dstrect);




    return 0;
  • \$\begingroup\$ Of course memory leaks. You never free texture in the loop. \$\endgroup\$ Jan 3, 2017 at 19:30

1 Answer 1


It's been a while since I've done any SDL but I think your problem is here: texture = SDL_CreateTextureFromSurface(renderer, surface); You're creating a texture and then never freeing it.

If you dig into the SDL2 source code you'll find that in SDL_render.c an SDL_Texture gets allocated on the heap every time you call SDL_CreateTexture* (texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));)

What will probably solve your leak, and save some frames, would be to only update the texture when the score updates and to free the existing texture before you allocate a new one.

if (prevScore != currentScore)
  //Create new score texture

//Regardless of score change, render the score
  • \$\begingroup\$ ok but what's with the prevScore and currentScore? \$\endgroup\$
    Jan 3, 2017 at 19:53
  • \$\begingroup\$ That's just one way to track score changes; just for the sake of example. Every frame you could change prevScore to the value of currentScore at the end of the frame and before that, if currentScore was changed during the frame, there was a change in score so you should update the texture. Or you could just have a dedicated method for updating score that would set a boolean like scoreChanged and if that was set to true you could update the score texture and then set scoreChanged back to false. \$\endgroup\$
    – Honeybunch
    Jan 3, 2017 at 19:54
  • \$\begingroup\$ I found the problem in my main game that was causing the memory leaks , I was using open font on every frame but never closing it. Now my game uses 60mb of Ram . The problem is it runs at 2fps , I think it's about what you said , it have to update the texture when the score updates. But I`m dumb and don't understand how to use the prevScore and current score. One last thing , can you do it for the example code I posted above? Please help me :(. \$\endgroup\$
    Jan 3, 2017 at 21:08
  • \$\begingroup\$ Like I said, the prevScore and currentScore variables were just to provide an example of how to detect a change in score. I suggest you open a new question with more code directly relevant to the specific frame drop issue you are having. \$\endgroup\$
    – Honeybunch
    Jan 3, 2017 at 21:42
  • \$\begingroup\$ I found why , normaly I have 2000 fps , when I load the font , create the surface , and create the texture every fps , it drops at 300 fps , because it happens every loop. Now I just have to detect a change in score and I don't know how :P . \$\endgroup\$
    Jan 3, 2017 at 21:49

You must log in to answer this question.

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