I create an array of size "N", then loop trough the array N times and set the values of the array and when I am finished using it I am trying to delete[] it. Then my program crashes and I am 100% sure I am not using anything out of the stack.
Code:
vec3 positions[numVertices];
vec2 textureCoords[numVertices];
for(unsigned int timesLooped = 0; timesLooped < numVertices; timesLooped++)
{
positions[timesLooped] = vertices[timesLooped].getPosition();
textureCoords[timesLooped] = textureCoords[timesLooped].getTextureCoord();
}
// Vertex position buffer.
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(positions[0]), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Vertex texture coordinates buffer.
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject[POSITION_TB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(textureCoords[0]), textureCoords, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
delete[] positions; // CRASHES
delete[] textureCoords; // CRASHES
I don't know why it crashes, but I just used vectors instead:
vector<vec3> positions;
vector<vec2> textureCoords;
positions.reserve(numVertices);
textureCoords.reserve(numVertices);
for(unsigned int timesLooped = 0; timesLooped < numVertices; timesLooped++)
{
positions.push_back(vertices[timesLooped].getPosition());
textureCoords.push_back(vertices[timesLooped].getTextureCoord());
}
// Vertex position buffer.
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(positions[0]), &positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Vertex texture coordinates buffer.
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject[POSITION_TB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(textureCoords[0]), &textureCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);