Handmade Hero»Forums»Code
19 posts
Deleting the OpenGL textures at the end
Edited by Terans on

In Handmade Hero, I don't see where Casey deletes all the allocated textures, so I have two questions:

  1. Isn't it necessary to delete all the OpenGL textures at the end of the program?
  2. In a more general purpose renderer, would it be a good idea to store all the allocated textures in the renderer itself, and delete all of them when the renderer is deinitialized? So we would not have to think about deleting them (except if we want to delete some of them manually during the course of the game, e.g. when unloading a level)

Because in SDL or Raylib, we have to manually delete all the allocated textures at the end, but why don't they keep track of the textures created so they can be deleted at the end when the renderer is freed?

Mārtiņš Možeiko
2562 posts / 2 projects
Deleting the OpenGL textures at the end
Edited by Mārtiņš Možeiko on

You never have to delete any resources when program terminates. OS will cleanup everything automatically: be it memory allocated, file handles, sockets, mutexes, threads, etc. That includes all GPU resources too - textures, shaders, vertex buffers, framebuffers, etc.

If this would not be true, can you imagine how bad it would be when you're developing your program? Like program crashes, nobody manually deleted textures. Or you're stepping in debugger and hard-terminate application, again nobody manually deleted textures. This way your computer would run out of GPU memory incredibly fast. Obviously this does not happen, because OS takes care of releasing all resources your program was using.

Cases where you want to free your textures during runtime of your program is when you're doing some kind of resource management. For example, unloading level and loading new one. Because old level textures won't be used anymore, you should release them to make space in memory for textures for the new level.

Same thing is with SDL & raylib. If you get to the point where application is terminating, there's no need to manually delete any SDL or raylib resource. Just exit program and you're done. Way quicker and simpler.

19 posts
Deleting the OpenGL textures at the end
Replying to mmozeiko (#25213)

Thank you for your answer, I always thought we had to free the resources at the end.