Hello everybody,
I’m experiencing a strange problem with glDrawArrays()on the Ipod Touch (OS version 2.0, OGL ES 1.1), and while I think I’ve already found a working solution, I still don’t really understand why it actually works. I was hoping somebody could clarify the following for me:
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 crash occurs as soon as I reach a certain amount of verts. Obviously the arrays 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…