PDA

View Full Version : Ok to free memory after glBufferData called



marshats
12-07-2009, 10:25 PM
Is it always safe to free memory on the CPU side once glBufferData called? For instance I have some code that calculates PositionData, normals, and texture coordinates once on start up (for use with gl3). This is then passed off to GL with a glBufferData call. I then want to free up the memory (PositionData,normals,texcoords).



GLfloat *PositionData=NULL;
GLfloat *normals=NULL;
GLfloat *texcoords=NULL;
GLsizei VertexCount = 0;

// fillin PositionData with malloc
// fillin normals with malloc
// fillin texcoords with malloc

// Generate a buffer object
glGenBuffers(1, &ArrayBufferName);
glBindBuffer(GL_ARRAY_BUFFER, ArrayBufferName);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*3*Ver texCount,PositionData,GL_STATIC_DRAW);
.. repeat for normals, texture cooridnates, etc


// cleanup memory from malloc (no longer needed on CPU side?)
if (PositionData) free(PositionData);
if (normals) free(normals);
if (texcoords) free(texcoords);


The code runs fine and draws what I expect even after I free the CPU side data. I just want to confirm this is proper to delete the data _immediately_ after glBufferData() called.

Alfonse Reinheart
12-08-2009, 12:11 AM
Is it always safe to free memory on the CPU side once glBufferData called?

Yes.

marshats
12-08-2009, 09:36 AM
Thanks. That is what it was acting like but wasn't certain in all cases if it was safe.