0
votes

Suppose I have 5 GLSL shaders that I successfully loaded in my opengl program. When I want to exit the game, of course I have to delete the shaders. So to delete the shaders, will I have to:

  1. Detach shader from program
  2. Delete shader
  3. Delete shader program

for every shader with glDeleteShader, glDeleteProgram, etc. one by one? Are there any easier and simple ways?

1
In regard to the "easier/simpler way", since C++11 we have shared pointers and can use RAII everywhere. That means you never really delete anything, when a variable goes out of scope, the destructor gets called and the cleaning happens automatically.Alexis Wilke

1 Answers

4
votes

Why are you waiting until the end of your application to delete your shader objects? You should have gotten rid of them after you linked the programs for them. Just attach them to the program, link it, detach them, and delete (unless you're reusing shader objects).

Once you're not carrying around the shader object baggage anymore, it's simple. Just delete the programs.

Or don't. It's up to you; the OpenGL context will clean up after itself. But if you want to delete them, then delete them.

If you have them in 50 different variables that store programs, and don't want to have to type glDeleteProgram 50 times, then it's pretty clear that your code is poorly structured for its size. If you have that many programs, then you need to procure a resource management system, where you can manage resources (like loaded programs) and ensure that they are destroyed. And by "procure," I mean "write."

Resource managers are basically boxes, where you put named objects into them and you get them out. When the resource manager is destroyed, then all of the resources it manages are destroyed too (note: there are ways to make a resource manager that can have alternate ways of destroying things).