title: glDrawElements() invalid write of size 4
body: <p>So I am drawing some text using C and OpenGL, it works really good but I found some problems while looking for memory leaks using Valgrind.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>void draw_text(
      const char* text,
      float x,
      float y,
      float z,
      float r,
      float g,
      float b,
      float size,
      float spacing,
      state_T* state
  )
  {
      float d_r = r / 255.0f;
      float d_g = g / 255.0f;
      float d_b = b / 255.0f;
  
      float vertices[] = 
      {
          // positions          // colors           // texture coords
          size,  size, 0.0f,   d_r, d_g, d_b,   1.0f, 1.0f,   // top right
          size, -size, 0.0f,   d_r, d_g, d_b,   1.0f, 0.0f,   // bottom right
          -size, -size, 0.0f,   d_r, d_g, d_b,   0.0f, 0.0f,   // bottom left
          -size,  size, 0.0f,   d_r, d_g, d_b,   0.0f, 1.0f    // top left
      };
  
      unsigned int indices [] =
      {
          0, 1, 3,   // first triangle
          1, 2, 3    // second triangle
      };
  
      glBindVertexArray(state-&gt;VAO);
      glUseProgram(SHADER_TEXTURED);
      send_projection_view_state(SHADER_TEXTURED, state-&gt;camera-&gt;projection_view);
  
      unsigned int VBO;
      glGenBuffers(1, &amp;VBO);
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
  
      for (int i = 0; i &lt; strlen(text); i++)
      {
          char c = text[i];
  
          unsigned int EBO;
  
          glGenBuffers(1, &amp;EBO);
  
          mat4 model =
          {
              1, 0, 0, 0,
              0, 1, 0, 0,
              0, 0, 1, 0,
              0, 0, 0, 1
          };
  
          glm_translate(model, (vec3){x, y, z});
  
          glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  
          glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
          glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  
          // position attribute
          glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
          glEnableVertexAttribArray(0);
          // color attribute
          glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3* sizeof(float)));
          glEnableVertexAttribArray(1);
  
          // texcoords
          glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
          glEnableVertexAttribArray(2);
  
          glActiveTexture(GL_TEXTURE0);
          //unsigned int tex = get_char_texture_from_texture(c, 7, 7, 8, 12, TEXTURE_DEFAULT_FONT);
          unsigned int tex = get_char_texture_from_texture(c, 8, 8, 16, 16, TEXTURE_LN_EGA8x8_FONT);
          glBindTexture(GL_TEXTURE_2D, tex);
          glUniform1i(glGetUniformLocation(SHADER_TEXTURED, "ourTexture"), 0); 
          glBindVertexArray(state-&gt;VAO);
  
          unsigned uniform_mat4_model = glGetUniformLocation(SHADER_TEXTURED, "model");
          glm_translate(model, (vec3){(i * (size + spacing)), 0, 0});
          glUniformMatrix4fv(uniform_mat4_model, 1, GL_FALSE, (float *) model);
  
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
          glDeleteBuffers(1, &amp;EBO);
          glDeleteTextures(1, &amp;tex);
      }
  
      glDeleteBuffers(1, &amp;VBO);
      glBindVertexArray(0); 
  }
  </code></pre>
  
  <p>Now, valgrind is telling me that here is a "invalid write of size 4" at the line where i call <code>glDrawElements</code>.</p>
  
  <p>Here is the exact valgrind output:</p>
  
  <pre><code>==29044== Invalid write of size 4
  ==29044==    at 0x7385901: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
  ==29044==    by 0x7390B8F: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
  ==29044==    by 0x737E8FB: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
  ==29044==    by 0x75BD48D: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
  ==29044==    by 0x75BE1A6: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_dri.so)
  ==29044==    by 0x10FE07: draw_text (draw_utils.c:365)
  </code></pre>
  
  <p>How do I solve this?</p>
  
  <p>I have tried fiddling with the arguments of the <code>glDrawElements</code> call but without any solution to the problem.</p>
  
  <p>Notes:
  The <code>get_char_texture_from_texture</code> just simply "cuts" out a character from a spritesheet font that I am using and binding it to a texture.</p>