Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when using TTF_CreateText #425

Open
bmrhoads opened this issue Oct 25, 2024 · 3 comments
Open

Memory leak when using TTF_CreateText #425

bmrhoads opened this issue Oct 25, 2024 · 3 comments

Comments

@bmrhoads
Copy link

I'm having an issue with this and I've isolated it to a simple project with a main like so

#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>

int main(int argc, char *argv[]) {
  int sdlVer = SDL_GetVersion();
  SDL_Window *window = 0;
  SDL_Renderer *renderer = 0;
  SDL_CreateWindowAndRenderer("Test", 900, 500, SDL_WINDOW_RESIZABLE, &window, &renderer);

  TTF_Init();
  TTF_TextEngine *engine = TTF_CreateRendererTextEngine(renderer);
  TTF_Font *font = TTF_OpenFont("dogica.ttf", 12);

  while (1) {
    TTF_Text *text = TTF_CreateText(engine, font, "Testing", sizeof("Testing"));
    SDL_RenderClear(renderer);
    TTF_DrawRendererText(text, 100, 100);
    SDL_RenderPresent(renderer);
    TTF_DestroyText(text);
  }

  TTF_CloseFont(font);
  TTF_DestroyRendererTextEngine(engine);
  TTF_Quit();
  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}

This process of creating the TTF_Text pointer and then destroying it seems to still leave something allocated because the memory grows at ~1MB a second. Maybe I'm misunderstanding something about how CreateText is meant to be used, I'm not sure.

This is on the newest build of the main branch

@slouken
Copy link
Collaborator

slouken commented Oct 25, 2024

I believe that should work. I'll double check.

@captain0xff
Copy link
Contributor

It may be unrelated but one thing I can spot is that sizeof("Testing") will include the null character, you probably don't want that.

@bmrhoads
Copy link
Author

bmrhoads commented Oct 26, 2024

Oh you're right, I should use strlen since CreateText appends the null terminator. I did notice that DestroyEngineText is not firing since these are new Text instances and text->internal->engine_text is always 0. Could it be that the engine->user_data is not being destroyed when the text is destroyed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants