我在我的項目中創建一個結構並返回指針的c函數。c內存泄漏問題
typedef struct object
{
float var1;
float var2;
}
Object;
Object *createObject(float newVar1, float newVar2)
{
Object *object; //create new structure
object = (Object*)malloc(sizeof(Object)); //malloc size for struct object
if(object != NULL) //is memory is malloc'd
{
object->var1 = newVar1; //set the data for var1
object->var2 = newVar2; //set the data for var2
return object; //return the pointer to the struct
}
return NULL; //if malloc fails, return NULL
}
現在的結構被使用過了一陣,我想刪除這個結構之後,我做了這個功能:
void deleteMarnix(Object *objectPointer)
{
free(objectPointer); //free the memory the pointer is pointing to
objectPointer = NULL; //stop it from becomming a dangling pointer
}
這最後的代碼段展示瞭如何使一個對象,使用它,並嘗試刪除它,但是,它似乎並沒有完全釋放內存。我究竟做錯了什麼?
Object *object = createObject(21.0f, 1.87f);
//do things here with object.
deleteMarnix(object);
你需要顯示你如何知道它泄漏,你可能會看到CRT預先分配。 – Puppy 2010-11-27 13:02:37
你是什麼意思,「它似乎並沒有完全釋放內存」? – 2010-11-28 12:34:05