-2

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);
  • 1
    Run it in a debugger and see where it stops responding. – Colonel Thirty Two Nov 09 '14 at 16:04
  • You're really not including the right kind of information, by the way. The operating system, particularly when discussing Windows, is the least important part of the equation. OpenGL support depends on the hardware vendor (e.g. Intel, AMD, nVidia) and driver version on Windows. Those are details that you should include when discussing a problem like this. – Andon M. Coleman Nov 09 '14 at 18:11
  • Found it! When I free the memory (delete[]) the program crashes. If I do not the program does not crash, but leaks occur. Why is that? Also delete[] is working just fine on my computer, but not on others. – Людмил Григоров Nov 09 '14 at 21:25
  • Probably some kind of memory management issue. Writing beyond the end of allocated memory, using freed memory, etc. – Reto Koradi Nov 10 '14 at 00:33
  • I used vectors and the problem is gone now. I am not doing anything like that. I create an array of size numVertices, then in a for loop set the values and all is ok, then I use them.. no crash and triangle is displayed correctly, but when i delete[] them it crashes... – Людмил Григоров Nov 10 '14 at 10:24

2 Answers2

2

positions and texturecoordinates are on the stack. delete commands are for freeing memory allocated with new ie for variables on the heap.

Stack:

{
   vec3 positions[numVertices]; //<--created on stack here


} <--- positions automatically freed at end of scope

Heap:

{
  vec3* positions = new vec3[numVertices]; //<--created on heap here


  delete [] positions; //<-- only freed when delete [] called

}<-- not freed at end of scope, would have memory leak without delete [] call

See eg Calling delete on variable allocated on the stack

Community
  • 1
  • 1
user3353819
  • 911
  • 2
  • 8
  • 21
1

You define arrays on stack and try to free that memory with delete[] which is illegal. You can only delete[] memory that you previously allocated with new[].

Paul
  • 13,042
  • 3
  • 41
  • 59