2012-07-04 139 views
0

我已經在我的OpenGL項目中實現了Bill Dudney的.Obj model loader,我目前有大量內存泄漏!OpenGLES釋放需要返回的變量

使用儀器,我設法縮小到下面的功能。我認爲這與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;

回答

0

你必須首先返回ptr,使用分配的數據,而不是釋放它。返回的ptr-s消費者應該在不再需要時或在應用程序退出時處理空閒。