# GL_INVALID_OPERATION on glBindVertexArray Despite glGenVertexArrays()

so I've been trying to create a simpler way to draw polygons and and have come across the following issue:

When I call glBindVertexArray(m_VAO); it throws a GL_INVALID_OPERATION even though I have previously called these functions:

glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);


Why does this happen? The only description for this error officially is that I failed to call the lines above.

Here is the source with any relevant parts. Please note that I have the error pop up the line after this one, but it might be because of this current error.

Polygon::Polygon(const std::vector<GLfloat>& vertexArray,
:m_vertexArray(vertexArray)
{
// ...
// ...

// Generate and bind vertex array object.
glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);

// Generate and bind vertex buffer object.
glGenBuffers(1, &m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);

//Send verticies to buffer's memory.
glBufferData(GL_ARRAY_BUFFER,
m_vertexArray.size() * sizeof(GLfloat),
&m_vertexArray[0],
GL_STATIC_DRAW);

// Set up vertex attributes.
glVertexAttribPointer(0,
3,
GL_FLOAT,
GL_FALSE,
3 * sizeof(GLfloat),
(GLvoid*)0);

// Enable the attributes at layout position 0.
glEnableVertexAttribArray(0);

//Unbind VBO and VAO.
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
};

// Destructor
Polygon::~Polygon()
{
// De-allocated resources
glDeleteVertexArrays(1, &m_VAO);
glDeleteBuffers(1, &m_VBO);
}

void Polygon::draw()
{
check_gl_error();
glBindVertexArray(m_VAO); // GL_INVALID_OPERATION here
check_gl_error();
glDrawArrays(GL_TRIANGLES, 0, m_vertexArray.size()); // GL_INVALID_OPERATION here
check_gl_error();
glBindVertexArray(0);
check_gl_error();
}


• Did you check that glGenVertexArrays succeeds and what's in m_VAO after the call? Also, do you have a GL3+ context? And how do you get your GL extension functions (like glBindVertexArray)? – snake5 Jul 2 '15 at 8:28
• There is no error output if I call check_gl_error(); after I create/bind it. Yes, my context is 3+(3.3). I get my functions with #include <GL/glew.h>. Thanks – Dennis Subachev Jul 2 '15 at 18:58
• Could it be that you're calling the destructor before "draw", perhaps by copying the instance of Polygon class? – snake5 Jul 2 '15 at 19:30
• That was exactly it. Do you want to post an answer so you can get credit? – Dennis Subachev Jul 2 '15 at 20:30

Since m_VAO is valid in constructor but not in draw(), it seems that the destructor was called before draw(), perhaps by copying an instance of the Polygon class.