glUseProgramm() throws 1281 error, eventhough it compiles and links correctly

Hi,
I am trying to implement a second pass for shadows - I am fairly positive that I have setup the FBO correctly for writing the depthbuffer information, however there is a problem with my shadowpass shaders: They compile and link without errors (checking with GL_LINK_STATUS and GL_VALIDATE_STATUS), whenever I call glUseProgram(correctShaderID) I get a 1281 error (GL_INVALID_VALUE and GL_INVALID_OPERATION). I cant figure out what the problem is, any hints you guys got for me?

I am creating the shaders in my Shader.cpp class and then push the object into std::vector shaderList - I have read that this could be the problem, however everything works fine for the shader at shaderList[1] - the errors occur for the shader stored at shaderList[0].

this is what my shadowpass shaders look like, are there any mistakes made?
Vertex:

#version 460

layout(location = 0) in vec3 pos;

uniform mat4 lightSpaceMatrix;
uniform mat4 model;

void main()
{
    gl_Position = lightSpaceMatrix * model * vec4(pos, 1.0);
}

Fragment:

#version 460

void main()
{
}

First, you need to identify exactly which command is generating the error; If you’re using glGetError (rather than KHR_debug) then you need to check that glGetError returns zero immediately before the command and non-zero immediately afterwards.

glUseProgram only generates GL_INVALID_VALUE (0x0501 = 1281) if its argument is neither zero nor the name of either a program object (generated by glCreateProgram) or a shader object (in which case, it will generate GL_INVALID_OPERATION instead). It won’t generate an error due to issues with the program itself so long as the program linked correctly (if it didn’t link correctly, glUseProgram will generate GL_INVALID_OPERATION).

If you’re actually getting GL_INVALID_VALUE from glUseProgram, check that its argument is a value returned from a prior glCreateProgram call.

I wrapped the glUseProgram() command with my checkerror() function, which is 0 before and 1 after the command was called:

checkError(); //nothing
shaderList[0].UseShader(); //glUseProgram(shaderID)
//shaderID was returned by glCreateProgram, which in this case was 1
checkError(); //error

I have also setup a glDebugMessageCallback which prints both 1281(GL_INVALID_VALUE) and 1282(GL_INVALID_OPERATION) for this line.

Maybe some helpful additional information:
I can retrieve the locations of my uniform varibles in my shader correctly, however GL_INVALID_OPERATION is thrown whenever I try to pass a value via glUniform…()

Thank you very much for your help - I have found the problem and it wasn’t related to openGL at all. I implemented it so that shaders get pushed into a std::vector shaderList - turns out the destructor of the shader was called when .pushback(shader) was called. All probs to this post!

If you’re going to wrap OpenGL objects in C++ objects, you need to follow the rule of three. OpenGL object names behave like pointers, so you have to treat them like pointers; any wrapper which performs allocation and deallocation realistically needs to behave like either std::unique_ptr (non-copyable, so can’t be stored in STL containers) or std::shared_ptr (reference counted, with the underlying object only deleted when the reference count reaches zero).

If you want to make them behave like copyable, non-pointer objects, the copy constructor has to perform a deep copy (i.e. extract the object state with the relevant glGet* functions and construct a new object with identical state), it can’t just copy the object name.

Oh, and if you’re going to call glDelete* in the destructor, you have to ensure that the correct context still exists and is bound when the destructor is called. This is non-trivial if you have multiple contexts, and even with a single context you have to be careful about objects which aren’t destroyed until main() terminates (i.e. objects with static storage duration, and anything referenced from them), because the context will probably have been destroyed at that point.