title: C++/OpenGL Random Segmentation Fault
body: <p>So I'm writing a chunk based procedurally generated terrain game and am running into two errors:</p>
  
  <p>Basically, the way it's working is it generates chunks around the player position, once per game loop.</p>
  
  <pre><code>for (int i = RENDER_RADIUS; i &gt;= 0; i--)
      {
          for (int j = RENDER_RADIUS; j &gt;= 0; j--)
          {
              terr.renderChunk(glm::ivec2(c-&gt;getXOff() + i, c-&gt;getZOff() + j), cubeShader);
              terr.renderChunk(glm::ivec2(c-&gt;getXOff() - i, c-&gt;getZOff() - j), cubeShader);
              terr.renderChunk(glm::ivec2(c-&gt;getXOff() - i, c-&gt;getZOff() + j), cubeShader);
              terr.renderChunk(glm::ivec2(c-&gt;getXOff() + i, c-&gt;getZOff() - j), cubeShader);
          }
      }
  </code></pre>
  
  <p>In terr.renderChunk I am using a unordered_map that uses the chunks position as the key and the chunk is the value. If the unordered_map doesn't find the chunk then the position gets added to terr.updateList.</p>
  
  <p>Then, back in the game loop:</p>
  
  <pre><code>if (!terr.updateList.empty())
      {
          terr.updateChunk(terr.updateList[terr.updateList.size()-1]);
          terr.world[terr.updateList[terr.updateList.size()-1]]-&gt;render(cubeShader);
          terr.updateList.pop_back();
      }
  </code></pre>
  
  <p>In a separate line, I'm ensuring that the players current chunk is loaded as well.</p>
  
  <p>To generate a chunks VBO I added the indices to the chunks vector points and then build it as so:</p>
  
  <pre><code>glGenVertexArrays(1, &amp;this-&gt;VAO);
  glBindVertexArray(this-&gt;VAO);
  
  // vertice VBO
  glGenBuffers(1, &amp;this-&gt;VBO_VERT);
  glBindBuffer(GL_ARRAY_BUFFER, this-&gt;VBO_VERT);
  glBufferData(GL_ARRAY_BUFFER, this-&gt;points.size() * sizeof(glm::vec3), &amp;this-&gt;points[0][0], GL_STATIC_DRAW);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  glEnableVertexAttribArray(0);
  
  // texture coords
  glGenBuffers(1, &amp;this-&gt;VBO_UV);
  glBindBuffer(GL_ARRAY_BUFFER, this-&gt;VBO_UV);
  glBufferData(GL_ARRAY_BUFFER, this-&gt;uvs.size() * sizeof(glm::vec2), &amp;this-&gt;uvs[0][0], GL_STATIC_DRAW);
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(0));
  glEnableVertexAttribArray(1);
  
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindVertexArray(0);
  </code></pre>
  
  <p>Now, it all generally works, but I randomly get a segmentation fault and have debugged it down to my render function:</p>
  
  <pre><code>void Chunk::render(Shader shader)
  {
      shader.setMat4("transform", offsetMatrix);
      shader.setFloat("transparency", 1.0f);
      glBindVertexArray(VAO);
      cout &lt;&lt; "size " &lt;&lt; points.size() &lt;&lt; endl;
      glDrawArrays(GL_TRIANGLES, 0, points.size()); //RIGHT HERE CAUSES THE SEGFAULTS
      cout &lt;&lt; "TEST2" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The segmentation fault seems to happen randomly, however, I do believe it doesn't happen on new chunks but rather going back over old ones.</p>
  
  <p>My question was, is there anything specific with OpenGL/C++ that I'm unaware of that could be causing it?</p>
  
  <p>The other error I'm getting that may be related but I've debugged less is I'm getting chunk rendering errors <a href="https://ibb.co/f5P18K" rel="nofollow noreferrer">as so</a> where it renders random terrain but when I go into it, collision still works as if terrain was where it should be.</p>
  
  <p>I realize this is a long question, but any support is really appreciated!</p>