私は構造体を作成してポインタを返す私のプロジェクトで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
"それは完全にメモリを解放していないようです"とはどういう意味ですか? –