J'ai une fonction c dans mon projet qui crée une structure et renvoie le pointeur.c problème de fuite de mémoire
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
}
maintenant la struct se habitue et après un certain temps que je veux supprimer cette structure, je l'ai fait cette fonction:
void deleteMarnix(Object *objectPointer)
{
free(objectPointer); //free the memory the pointer is pointing to
objectPointer = NULL; //stop it from becomming a dangling pointer
}
ce dernier extrait de code montre comment je fais un objet, l'utiliser et essayer pour le supprimer, cependant, il semble qu'il ne libère pas complètement la mémoire. Qu'est-ce que je fais mal?
Object *object = createObject(21.0f, 1.87f);
//do things here with object.
deleteMarnix(object);
Vous devez montrer comment vous savez qu'il fuit, vous pourriez être voir pré-allocation par le CRT. – Puppy
Que voulez-vous dire par "il semble qu'il ne libère pas complètement la mémoire"? –