C complex memory leak-Collection of common programming errors
I’m having a slight memory leak in my program and i’m not sure if it’s in my allocs or in the internal c structures. The only mallocs i’m using are these:
results = (int*) malloc (instance_n * sizeof (int) );
instances = (char**) malloc (instance_n * sizeof (char*) );
for (i = 0; i < instance_n; i++) {
instances[i] = (char*) malloc (1001 * sizeof (char) );
}
List_add (); (standard doubly linked list. Never gave me a problem)
And i free everything in the same place:
free (results);
List_clear (&dynamic);
for (i = 0; i < instance_n; i++) {
free (instances[i]);
}
free (instances);
BTW: List_clear =
Node* node = list->last;
if (node == NULL) return;
while (node->previous != NULL)
{
node = node->previous;
free (node->next);
}
free (list->first);
Additionally, i’m using timeval and FILE structures (the files are fclosed at the end of the methods)
Am I missing something? To me it most certainly looks like i’m freeing everything. I never had a memory leak problem before so I’m terrible at debugging it, but Valgrind keeps pointing out this memory leak:
==3180== HEAP SUMMARY:
==3180== in use at exit: 62,951 bytes in 361 blocks
==3180== total heap usage: 556 allocs, 195 frees, 115,749 bytes allocated
==3180==
==3180== LEAK SUMMARY:
==3180== definitely lost: 8,624 bytes in 14 blocks
==3180== indirectly lost: 1,168 bytes in 5 blocks
==3180== possibly lost: 4,925 bytes in 68 blocks
==3180== still reachable: 48,234 bytes in 274 blocks
==3180== suppressed: 0 bytes in 0 blocks
==3180== Rerun with --leak-check=full to see details of leaked memory
==3180==
I can’t help but notice the “14 blocks” part, but no part of my code allocates less than 20 parts and 8624 bytes is a multiple of 4 bytes so it’s most likely an integer leak.
Thanks in advance