Memory management OpenGLES发布释放变量,然后需要返回该变量

Memory management OpenGLES发布释放变量,然后需要返回该变量,memory-management,opengl-es,free,opengl-es-2.0,Memory Management,Opengl Es,Free,Opengl Es 2.0,我已经在我的一个OpenGL项目中实现了Bill Dudney的.Obj,目前有大量内存泄漏 使用仪器,我设法将其缩小到下面的函数。我认为这与ptr从未被释放有关,但我仍然需要在函数末尾返回ptr const void * TextureCoordBytes(CFAllocatorRef allocator, const void *value, GLuint *size) { // extract the count GLuint count = ((GLuint *)value)[0]; v

我已经在我的一个OpenGL项目中实现了Bill Dudney的.Obj,目前有大量内存泄漏

使用仪器,我设法将其缩小到下面的函数。我认为这与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;
}

有没有人知道我在这里遗漏了什么,或者知道我可以免费使用(ptr)的方法和仍然
返回ptr之后?

您必须首先返回ptr,使用分配的数据,然后释放它。当不再需要或在应用程序退出时,返回ptr-s的使用者应处理释放