void allocs_atexit(void) { RSNode nd; void *r; if (!test_leaks) return; #ifdef DEBUG if (RSTree_count(allocs_tree) > 0) { int i = 1; printf(">> Memory leaks: "); nd = RSTree_first(allocs_tree); while (nd) { r = (void *)RSNode_get_key(allocs_tree, nd); printf("[#%d %d \"%.20s\"] ", i, RSNode_get_val(allocs_tree, nd), (char *)r); original_free(r); nd = RSTree_next(allocs_tree, nd); i++; } printf("\n"); } else printf(">> No memory leaks found (%d mem operations).\n", memop); #endif /* DEBUG */ RSTree_destroy(allocs_tree); }
void my_free(void *r) { #ifdef DEBUG register_atexit(); if (!RSTree_remove(allocs_tree, (int)r)) allocs_fatal("free for non-allocated chunk"); memop++; #endif /* DEBUG */ original_free(r); }
void free(void *ptr){ int i; for(i=0;i<=max;i++){ if(locs[i]==ptr){ locs[i]=NULL; sizes[i]=0; } } original_free(ptr); }
void free(void *ptr) { extern int nb_times_free_used; nb_times_free_used++; //On enregistre le nombre d'utilisations de malloc // On déclare un pointeur vers une fonction qui a le même prototype void *(*original_free) (void *ptr); // On crée une copie du pointeur vers la fonction malloc originale original_free = dlsym(RTLD_NEXT, "free"); // On exécute la fonction malloc originale, car exécuter malloc(size) // reviendrait à faire un appel récursif infini ... void *pt = original_free(ptr); if (pt == NULL) // Dans le cas où malloc aurait réellement échoué return NULL; }