I have a huge memory leak which is making me question my implementation of SDL_TTF
as a whole.
Right now I have the following classes
MainMenu
Menu
Options
Button
Text
The MainMenu
and Option
classes abstract the menu class and each call their own button class with parameters that build the text class.
So right now my main loop passes the SDL_Renderer
(gRenderer) down through a MainMenu->Button->Text path.
When my Text class is initialized it loads the font, sets up the font using TTF_RenderText_Solid
with the message that has been passed in by the Button class, and the TTF_SetFontStyle
to set the text bold.
I draw using:
void Text::draw(SDL_Renderer* gRenderer)
{
renderQuad = { getXPosition(), getYPosition() };//, getWidth(), getHeight()};
mTexture = SDL_CreateTextureFromSurface(gRenderer, message);
SDL_RenderCopyEx(gRenderer, mTexture, NULL,
&renderQuad, NULL, NULL, SDL_FLIP_NONE);
};
I believe it is bloating on SDL_CreateTextureFromSurface
or SDL_RenderCopyEx
.
What can I do to amend this memory leak? Am I overlooking something blatant?
Solution:
Discovering/Using Twinklebear's ifnroamtion:
When declaring the object in the header, I declared the texture and set it equal to NULL
By: SDL_Texture* mTexture = NULL;
Then, inside of the draw function I check if the texture is set to NULL or not:
void Text::draw(SDL_Renderer* gRenderer)
{
renderQuad = { getXPosition(), getYPosition(), message->w, message->h };
if (mTexture == NULL)
{
mTexture = SDL_CreateTextureFromSurface(gRenderer, message);
}
SDL_RenderCopy(gRenderer, mTexture, NULL, &renderQuad);
};
What I would like to do is throw a boolean variable on the class to state if the texture needs updated or not, and reset the texture.