title: glDrawElements VAO/VBO crash on iOS
body: <p>I am creating a batching class that uses VAOs and VBOs to manage meshes.  However, when attempting to use glDrawElements, I get EXEC_BAD_ACCESS and GL_INVALID_OPERATION when binding back to my VAO.  Here is the code:</p>
  
  <pre><code>glGenVertexArraysOES(1, &amp;arrayID);      
  glBindVertexArrayOES(arrayID);    // Bind INTO VAO, opening state
  
  // Load shaders and textures and bind them using glUseProgram etc.
  
  glGenBuffers(1, &amp;vboID);
  glBindBuffer(GL_ARRAY_BUFFER, vboID);
  glGenBuffers(1, &amp;indexID);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID); 
  glBindVertexArrayOES(0);    // Bind AWAY from VAO, saving state
  
  Glfloat data[length];
  
  glBindVertexArrayOES(arrayID);    // Bind INTO VAO, open state
  
  unsigned int glfloatsize = sizeof(GLfloat);
  unsigned int stride = kStride * glfloatsize;
  
  // Fill Vertex information
  glBufferData(GL_ARRAY_BUFFER, vertCount * glfloatsize * kStride, NULL, GL_STATIC_DRAW);
  glBufferSubData(GL_ARRAY_BUFFER, 0, vertCount * glfloatsize * kStride, data);   
  
  glEnableVertexAttribArray(kPositionLocation);
  glVertexAttribPointer(kPositionLocation, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(0));
  
  glEnableVertexAttribArray(kNormalLocation);
  glVertexAttribPointer(kNormalLocation, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(3));
  
  glEnableVertexAttribArray(kColorLocation);
  glVertexAttribPointer(kColorLocation, 4, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(6));
  
  glClientActiveTexture(GL_TEXTURE0);
  glEnableVertexAttribArray(kTextureLocation);
  glVertexAttribPointer(kTextureLocation, 2, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(10));
  
  // Fill Index information
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(GLushort), NULL, GL_STATIC_DRAW);
  glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexCount * sizeof(GLushort), index);
  
  glBindVertexArrayOES(0);    // Bind AWAY from VAO, saving state
  
  // DO OTHER STUFF
  
  /** RENDER (EXPLODES WITH EXEC_BAD_ACCESS) **/
  glBindVertexArrayOES(arrayID);
  glDrawElements(renderMode, indexCount, GL_UNSIGNED_SHORT, 0);
  glBindVertexArrayOES(0);
  
  /** RENDER (WORKS CORRECTLY [index is a scoped array of GLushorts that are uploaded to the VBO above...]) **/
  glBindVertexArrayOES(arrayID);
  glDrawElements(renderMode, indexCount, GL_UNSIGNED_SHORT, index);
  glBindVertexArrayOES(0);
  </code></pre>
  
  <p>Any idea why I am receiving EXEC_BAD_ACCESS when attempting to use a GL_ELEMENT_ARRAY_BUFFER VBO?</p>