Hi everybody,
while I think I've already found a working solution for the problem, I still don't really understand why it actually does work. I was hoping somebody could clarify the following for me.
I was trying this on an ipod touch (OpenGL ES 1.1).
simplified code:
void Foo::drawSome()
{
GLfloat *vertices = new GLfloat[strLength*12];
GLfloat *coordinates = new GLfloat[strLength*18];
// fill with correct verts and coords...
// draw stuff
glPushMatrix();
glTranslatef(x, y, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_fontTex.name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLES, 0, strLength*6);
glPopMatrix();
// here it crashes, basically complaining that I'm freeing memory still in use
delete [] vertices;
delete [] coordinates;
}
The app crashes as soon as I reach a certain amount of verts. Obviously the array are still in use when I try to free the memory.
The solution is to use static arrays like this:
void Foo::drawSome()
{
GLfloat vertices[256*18];
GLfloat coordinates[256*12];
// fill with correct verts and coords...
// draw stuff
glPushMatrix();
glTranslatef(x, y, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_fooTex.name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLES, 0, strLength*6);
glPopMatrix();
}
I don't understand why this works and dynamically allocated arrays don't. Shouldn't the static arrays be gone as soon as the function returns?
So what's the real difference? Does the data on the stack live longer? Long enough for it to work?
Please explain...