OpenGLES问题释放需要返回的变量

时间:2012-07-04 16:39:34

标签: memory-management opengl-es free opengl-es-2.0

我已经在我的OpenGL项目中实施了Bill Dudney的.Obj model loader,目前我正在进行大量的内存泄漏!

使用乐器,我设法将其缩小到以下功能。而且我认为它与ptr的关系永远不会被释放,但我仍然需要在函数结束时返回ptr。

const void * TextureCoordBytes(CFAllocatorRef allocator, const void *value, GLuint *size)
{
// extract the count
GLuint count = ((GLuint *)value)[0];
void *ptr = NULL;
if(1 == count)
{ // a 1D texture
    ptr = CFAllocatorAllocate(allocator, sizeof(GLfloat), 0);
    ((GLfloat*)ptr)[0] = ((TextureCoord1D *)value)->u;
    *size = sizeof(GLfloat);
}
else if(2 == count)
{ // a 2D texture
    ptr = CFAllocatorAllocate(allocator, 2 * sizeof(GLfloat), 0);
    ((GLfloat*)ptr)[0] = ((TextureCoord2D *)value)->u;
    ((GLfloat*)ptr)[1] = ((TextureCoord2D *)value)->v;
    *size = 2 * sizeof(GLfloat);
}
else if(3 == count)
{ // a 3D texture
    ptr = CFAllocatorAllocate(allocator, 3 * sizeof(GLfloat), 0);
    ((GLfloat*)ptr)[0] = ((TextureCoord3D *)value)->u;
    ((GLfloat*)ptr)[1] = ((TextureCoord3D *)value)->v;
    ((GLfloat*)ptr)[2] = ((TextureCoord3D *)value)->w;
    *size = 3 * sizeof(GLfloat);
}
return ptr;
}

有没有人知道我在这里失踪的东西,或者我可以使用free(ptr);而后仍然return ptr;的方式?

1 个答案:

答案 0 :(得分:0)

你必须首先返回ptr,使用分配的数据,而不是释放它。退回的ptr-s的消费者应该在不再需要时或在app退出时处理释放。