I have created a wrapper object for the SDL_ttf font. This object I created has a string for the message to be displayed and also an SDL_Texture. I will update the string and then re-create the texture from the new string passed in and I noticed that this causes a memory leak. I’m trying to call SDL_DestroyTexture to resolve the memory leak. Sometimes though when I call SDL_QueryTexture(Message, NULL, NULL, NULL, NULL) I get a segmentation fault, but not in all cases. I’m calling SDL_QueryTexture to see if the Texture has already been set and if it is, destroy it and then create a new one.
SDL_Surface* surfaceMessage;
surfaceMessage = TTF_RenderText_Solid(Classic, textMessage.c_str(), White);
Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
// SDL_DestroyTexture(Message); // Adding this line will resolve the memory leak but obviously wont display the font anymore.
SDL_FreeSurface(surfaceMessage);
I’m trying something like this to remove the old texture before creating the new one.
if(SDL_QueryTexture(Message, NULL, NULL, NULL ,NULL) == 0) {
SDL_DestroyTexture(Message);
}
SDL_Surface* surfaceMessage;
surfaceMessage = TTF_RenderText_Solid(Classic, textMessage.c_str(), White);
Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_FreeSurface(surfaceMessage);
I’m initializing this Font object that I created like this:
//initialize the fonts for line, level, score
fontOne = { renderer, LINES_POS_X_PX, LINES_POS_Y_PX, 100, 35 };
ptrFont = { &fontOne };
fontLvl = { renderer, LVL_POS_X_PX, LVL_POS_Y_PX, 100, 35 };
ptrFontLvl = { &fontLvl };
fontScore = { renderer, SCORE_POS_X_PX, SCORE_POS_Y_PX, 100, 35 }; //Seg fault here!!!
ptrFontScore = { &fontScore };
mRenderables.push_back(&fontOne);
mRenderables.push_back(&fontLvl);
mRenderables.push_back(&fontScore);
updateScores();
The pointers are used to update the font later.
The problem is for some reason I get a Segmentation Fault when creating the fontScore. The other cases before that seem to work ok. I notice that when I get the Segmentation Fault the Message is 0x6, I would either expect 0x0 or a valid Message, but can’t figure out why I’m seeing 0x6.
I hope you may be able to provide some insight into how I can create an updateable font without causing a memory leak.
Electrosys