• Advertisement
Sign in to follow this  

SDL TTF memory leak

This topic is 3739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to write a simple text into my game app with SDL_ttf, and so far it works, but I noticed that the games frequently increases its memory demand after I built in SDL_ttf. Can anyone tell me what I did wrong?
void Game::run ()
{
  while (gameRun == true)
  {
    processEvents ();

    SDL_GetMouseState(&mouseX, &mouseY);

    pointer_Framework->update ();
    pointer_Framework->clear ();

    battlefield.render_starsystems(mouseX,mouseY);

    pointer_text->write("Hello",1170,100);

    pointer_Background->render ();

    pointer_Cursor->setPos(mouseX, mouseY);
    pointer_Cursor->render ();
	
    pointer_text->render();
    pointer_Framework->flip ();

  }

}
void Truetype::write      	(const char* text, float xPos, float yPos)
{
	pointer_image = TTF_RenderText_Blended(pointer_font, text, fontcolor );
	rect.x = static_cast<int> (xPos);
	rect.y = static_cast<int> (yPos);
}

Share this post


Link to post
Share on other sites
Advertisement
TTF_RenderText_* creates a surface which you must free after using it, like any other.


void Truetype::write (const char* text, float xPos, float yPos)
{
pointer_image = TTF_RenderText_Blended(pointer_font, text, fontcolor );
rect.x = static_cast<int> (xPos);
rect.y = static_cast<int> (yPos);
SDL_FreeSurface(pointer_image); // or somewhere else... just dont let it leak
}


Edit: Since you are rendering the text here and blitting it at another point of the code, you could free the old surface before rendering and on object's destruction. This way, you don't need to group text rendering and blitting.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement