SDL_DestroyTexture() and GPU memory issus

I made a simple code to display system date as a texture

label2.c (3.3 KB)

In the main loop I create the surface then transform it to texture to display. After the rendering I destroy the texture and the surface.

If I do not destroy the texture, the GPU memory grows till completely full, but the system keeps running.

Should I not remove the surface? Is that some kind of caching managed by SDL and/or GPU?

I also would like to hear any other useful tips to improve in such small code.

For those who need, the font used is a Free Font available at

Yes, you’re correct - every SDL_Surface and SDL_Texture created by the developer should be destroyed with corresponding SDL_FreeSurface/SDL_DestroyTexture function call. Not following that will result in memory leak(s).

Why the GPU (or the program) didn’t crash for unavailable memory?

What unavailable memory are you referring to?

My GPU has a limited amount of memory, If I keep allocating textures, without destroying them, I would expect the program crashes for lack of resources. In this case GPU memory.

But this do not happens.

User space programs don’t have any control over what is actually using GPU memory. If you require more storage than your hardware has the driver will happily stream unused textures out of the GPU into system RAM so it can load textures it will actually need.

Nice to know, as game developer should I keep an eye on GPU memory usage? Or is perfectly fine let as is?

No, you should definitely avoid using more than you have. It is extremely slow to stream data back and forth from the GPU and you want to avoid making things slow to a crawl (among other things). Allocating textures without freeing them is a resource leak that serves no good. It leaks more than GPU memory anyway. Eventually something would give, but that depends on implementation details and shouldn’t matter if you have a well behaved program.