Proper way to delete GLSL shader?

My code approaches GLSL shader management in the way, that it creates each shader and the associated program and deletes each shader and program. I recently read http://www.opengl.org/wiki/GLSL_Object and there it is stated that:

The shader object, due to being attached to the program object, will continue to exist even if you delete the shader object. It will only be deleted by the system when it is no longer attached to any program object (and when the user has asked to delete it, of course).

Do I get this correctly, if I call glDeleteShader() on the shader object after linking to the program, I only need to track the program? Is it safe to assume this is always true?


Solution 1:

Yes -- in fact it is highly desireable to detach and delete your shader objects as soon as possible. That way the driver can free up all the memory it is using to hold a copy of the shader source and unlinked object code, which can be quite substantial. Measurements I have done indicate that NOT deleting the shader objects increases the incremental memory use per shader by 5-10x

Solution 2:

In general, the way shader object management works is simple. Shader objects don't really do anything, so there's no point in tracking them at all. Shader objects should exist for just long enough to successfully link a program object. After which time, the shaders should be detached from the program and deleted.

The above assumes that you aren't trying to use the shader object to link with a different program, of course. That's certainly possible. In that case, you should delete your shader objects after you have linked all of your programs.