Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

After I have a linked program, and thinking about defensive programming, should I delete and detach the shaders used to link this program?

If yes, is that going to free any resources? Or do these shader objects only going to be freed after a glDeleteProgram call?

edit: Just for clarification what I am doing is (which is consistent with the answer):

glCreateShader -> glShaderSource -> glCompileShader -> glCreateProgram -> glAttachShader -> glLinkProgram -> glDetachShader -> glDeleteShader -> draw using this shader program -> and when I don't need this shader anymore glDeleteProgram

share|improve this question
up vote 36 down vote accepted

Yes, you should always do this. I didn't find out about this until just recently, but a shader won't actually be deleted by glDeleteShader until it's been detached. It's mentioned on the man page for glDetachShader

EDIT: Almost missed the bit about deleting the shaders too. Yes, you should do this as it frees up the memory used to store the shader source and unlinked object code. This is explained in more detail in this StackOverflow question.

share|improve this answer
+1 and it also makes cleanup easier as you've only one object to track and glDelete. – Le Comte du Merde-fou Jan 28 '13 at 0:46

The order is right, but you do not really need to deattach the shaders, because calling glDeleteProgram does that for you with all attached shaders and then they are automaticaly deleted (if they were flagged for deletion with glDeleteShader func) .

Reference: .

share|improve this answer
Shader objects take up a lot of memory compared to each program. It's better to delete them after you're done compiling your programs and have more memory available for more important things. – Robert Rouhani Sep 9 '13 at 15:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.