I am trying to edit text in SDL2 TTF, and I noticed as i was adding/subtracting text, the memory was not going down. The problem may possibly be in the SDL_DestroyTexture
and SDL_FreeSurface
but its also posing an issue with loading fonts. The way I have text is after the first instance of text, I load the fonts required. Then when the last instance is "free'd" i close the fonts. Here is the code for it (Individual_Characters
is my class for text, basically an array of them)
Loading the texture
void Individual_Characters::Load_Character_Texture(SDL_Renderer *renderer)
{
if (characterTexture != NULL){
SDL_DestroyTexture(characterTexture);
characterTexture = NULL;
}
if (character != "")
{
SDL_Surface *characterSurface = NULL;
if (bold == true){
characterSurface = TTF_RenderText_Blended(fontsBold[fontIndex], character.c_str(), characterColour);
}
if (bold == false){
characterSurface = TTF_RenderText_Blended(fonts[fontIndex], character.c_str(), characterColour);
}
if (!characterSurface){
std::cout << "Could not create surface " << SDL_GetError() << std::endl;
}
else{
characterTexture = SDL_CreateTextureFromSurface(renderer, characterSurface);
SDL_FreeSurface(characterSurface);
characterSurface = NULL;
}
SDL_QueryTexture(characterTexture, NULL, NULL, &characterRect.w, &characterRect.h);
}
else {
characterRect.w = 0;
characterRect.h = 0;
}
}
Setting up the individual character
void Individual_Characters::Setup(SDL_Renderer *renderer, std::string p_character, int p_x, int p_y, int p_characterSize, SDL_Colour p_colour, bool p_bold, bool p_clickable, std::string p_fontType)
{
if (setup == false){
characterRect.x = p_x;
characterRect.y = p_y;
character = p_character;
characterFont = p_fontType;
characterSize = p_characterSize;
characterColour = p_colour;
clickable = p_clickable;
bold = p_bold;
fontIndex = characterSize - lowestFontSize -1;
numOfInstances += 1;
if (numOfInstances == 1){
std::cout << "Character instance where initialising fonts " << character << std::endl;
TTF_Init();
fonts.reserve(highestFontSize-lowestFontSize+1);
fontsBold.reserve(highestFontSize-lowestFontSize+1);
for (int i=lowestFontSize; i <= highestFontSize; i++)
{
TTF_Font *currentFont = TTF_OpenFont(characterFont.c_str(), i);
TTF_Font *currentFontBold = TTF_OpenFont(characterFont.c_str(), i);
TTF_SetFontStyle(currentFontBold, TTF_STYLE_BOLD);
fonts.push_back(currentFont);
fontsBold.push_back(currentFontBold);
}
}
Load_Character_Texture(renderer);
}
setup = true;
}
and Then when i "backspace" I call this first and pop_back my vector after of Individual_Characters
after.
void Individual_Characters::Free_All()
{
if (setup == true){
if (characterTexture != NULL){
SDL_DestroyTexture(characterTexture);
}
if (numOfInstances == 1){
std::cout << "Character instance where closing fonts " << character << std::endl;
for (int i=0; i < fonts.size(); i++){
TTF_CloseFont(fonts[i]);
TTF_CloseFont(fontsBold[i]);
}
fonts.clear();
fontsBold.clear();
TTF_Quit();
}
numOfInstances -= 1;
}
setup = false;
}
numOfInstances
is how many instances of Individual_Characters
I have, as I delete all the existing text, the output lets me know that the fonts were, in fact, closed. Regardless of this. Before text is loaded, the program sits at about 30mb
ram, as the fonts load, 70mb
ram. This is fine, but as the program uses the TTF_CloseFont()
, the memory still remains at 70mb even AFTER it is freed.