This is the scenario: I have a class named Program, which holds three shared_ptr: to the vertex, geometry and fragment shader. When a Shader object gets constructed, it creates the shader with glCreateShader, and it also compiles it.
The Shader denstructor automatically calls glDeleteShader. So the problem is that if I do the following:
- Create a shader object;
- Copy it;
- Destroy the copy.
Also the original copy gets invalidated, because when the copy is destroyed it calls glDeleteShader. That's a design problem, I believe.
So I avoided this problem by just using pointers. Now the Program class holds the shaders. I created a method that returns the shared_ptr to the vertex, geometry and fragment Shader objects. My doubt is: should I return the shared_ptr like this:
const shared_ptr<Shader>& getVertex_shader_ptr() const
{
return vertex_shader_ptr;
}
Or like this:
shared_ptr<Shader> getVertex_shader_ptr() const
{
return vertex_shader_ptr;
}
My fear is that the problem that I described above occurs again: the shared_ptr gets deallocated and it invalidates the OpenGL shader.