Reputation: 335
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.
Upvotes: 3
Views: 291
Reputation: 3274
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.
Upvotes: 4