3

I want to print health information on my screen for a game using SDL ttf but i get a memory leak.

The game starts and works for a while (with text and all) but after a few seconds it stops.

Normally you should free the textSurface after running SDL_RenderCopy but even after doing this it still does not seem to work.

(i have tested the rest of the code and find out i only get the memory leak after using renderHealth so im 100% sure this is causing the problem.)

SDLText.h:

class SDLText {

    SDL_Surface* textSurface;
    SDL_Texture* text;
    TTF_Font * font;
...
}

SDLText.cpp:

void SDLText::renderHealth( int health) {

    font = TTF_OpenFont("fonts/OpenSans-Regular.ttf", 80);
    if (font == NULL) {
        printf("font error");
    }

    std::string score_text = "health: " + std::to_string(health);
    SDL_Color textColor = {255, 255, 255, 0};
    textSurface = TTF_RenderText_Solid(font, score_text.c_str(), textColor);
    text = SDL_CreateTextureFromSurface(gRenderer, textSurface);


    SDL_Rect Message_rect; //create a rect
    Message_rect.x = 120;  //controls the rect's x coordinate
    Message_rect.y = 5; // controls the rect's y coordinte
    Message_rect.w = 100; // controls the width of the rect
    Message_rect.h = 20; // controls the height of the rect

    SDL_RenderCopy(gRenderer, text, NULL, &Message_rect);

    SDL_FreeSurface(textSurface);
    SDL_DestroyTexture(text);

}

Can someone tell me what im not seeing/missing?

SOLUTION: After adding TTF_CloseFont(font); at the end my problem was solved.

Albin Gjoka
  • 305
  • 1
  • 2
  • 14

1 Answers1

4

The font is opened but never closed. Use TTF_CloseFont to free the memory used by font.

Moreover, you should consider avoiding opening the font every font every time you want to render.

Bilal Ahmed
  • 381
  • 2
  • 13
  • Relating to font being opened, if can't seem to open it at the begining because when i callrenderHealth i get an error telling me that it failed to open font... If i do this it wont work. class SDLText { SDL_Surface* textSurface; SDL_Texture* text; TTF_Font * font = TTF_OpenFont("fonts/OpenSans-Regular.ttf", 80); ... } – Albin Gjoka May 21 '19 at 06:55
  • @AlbinGjoka It may depends on when the `SDLText` class is instantiated. It should be after `SDL_Init` and `TTF_Init`. Alternatively (but I do not recommend it), perform a lazy initialization, i.e., initialize `font` to `nullptr` when you create the class and call `TTF_OpenFont` when you call `renderHealth` if the pointer is null. Then, in your destructor, call `TTF_CloseFont` is the pointer is not null. I do not recommend it because it can slow down the first call to `renderHealth` and because it also means that you don't have control on the order of operation which can be a sign a bad design – Gilles-Philippe Paillé May 21 '19 at 12:52