Memory leak with TTF font

Hi everyone, I am new with SDL2
There are memory leak in my code, but I can’t understand

This is my code:

void Game::draw(std::string msg, int x, int y, SDL_Color color) {

SDL_Surface* surf = TTF_RenderText_Solid(font, msg.c_str(), color);
SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);

SDL_Rect rect;
rect.x=x;
rect.y=y;
rect.w=surf->w;
rect.h=surf->h;
SDL_FreeSurface(surf);

SDL_RenderCopy(ren, tex, NULL, &rect);
SDL_DestroyTexture(tex);

}

In main loop I call function draw()
When I run my code, at first it uses 10M but after 1 second it uses 13M, then 17M… finnally it uses 27M and don’t change anymore.

I was free surface and destroy texture.

I try to remove line SDL_RenderCopy(ren, tex, NULL, &rect), everything is OK, don’t have memory leak anymore.
I thinks problem from line SDL_RenderCopy. But I don’t know why?

Tks.

Which platform are you on? Typically, the memory assigned and measured by the operating system (e.g. viewing process memory usage on Windows via Task Manager) is not exactly what your program is currently using, but is what the OS has made available to your program. This value may not instantly reflect what your program is doing, as the OS decides when that value changes, not your program. For example, it takes some computation time to release memory for use by other applications and the OS may not immediately do that when there’s plenty of free memory elsewhere.

In your case, the memory usage stabilizes, which indicates that it is likely memory usage that the viewer is catching up on, not an unrecoverable memory leak.

1 Like