0

im trying to free this linked list but for some reason visual studio memory usage always shows me that 5 or 6 extra allocations stay with each iteration of the program.

this is the function i use to create the linked list :

int index = 0, c = 0, nodes_count = 0, d = info.height, maxtempx = -1, firstscan = 1, maxtempy = -1, mintempx = 502, mintempy = 502;
    chargers_list_t* head = NULL;
    chargers_list_t* temp = malloc(sizeof(chargers_list_t));
    if (temp == NULL)
    {
        printf_s("error creating list\n");
        return NULL;
    }
    for (int i = info.height - 1; i >= 0; i--) {
        for (int j = 0; j < info.width; j++) {
            if (matrix[i][j] == -1) {
                index++;
                group_chargers(matrix, i, j, index, info);
                chargers_list_t* temp = malloc(sizeof(chargers_list_t));
                if (temp == NULL) {
                    printf_s("error allocation\n");
                    return NULL;
                }
                temp->index = index;
                temp->next = NULL;
                if (head == NULL)
                    head = temp;
                else {
                    chargers_list_t* tail = head;
                    while (tail->next != NULL) {
                        tail = tail->next;
                    }
                    tail->next = temp;
                }
                
            }
        }
    }

and this is the function i use to free it:

oid freeList(chargers_list_t* head) {
    if (head == NULL) {
        return;
    }
    freeList(head->next);
    free(head);
}

i tried freeing it both in a while loop and recursively but the result stays the same

Code Bom
  • 5
  • 1
  • 1
    I think the problem is in how you allocate the list; for example, consider what happens with the very first allocation. – Scott Hunter Jun 22 '23 at 20:48
  • 5
    You have two `temp` declarations. You don't seem to refer to the outer one after having allocated it. – 500 - Internal Server Error Jun 22 '23 at 20:49
  • now that i deleted the first temp malloc i got one less allocation in the code, is it suppose to be worth 1 allocation? asking since im a total noob and the other allocations are unresolved (ofc it can be part of a different section of my actual code which is much bigger)? – Code Bom Jun 22 '23 at 20:54
  • you can run your code under valgrind, it helps you find leaked memory. There's also the question of whether you [_really_ need to cleanup](https://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc-before-program-termination?rq=2), depends on your specific situation. But in general, you need a corresponding `free` for every `malloc`. If you don't have that, you're going to leak. – yano Jun 22 '23 at 21:08
  • Try a smaller matrix, even 1x1. That's small enough to easily step thru each `malloc` and `free` for it. If still leaking, the problem is elsewhere. – yano Jun 22 '23 at 21:14
  • Please show enough code for us to reproduce the problem. You write *"this is the function..."*, but then omit the function header, and how you call that function. `freeList` is given, but the call to it is lacking. – trincot Jun 22 '23 at 21:25
  • @Code Bom You need more practise! This declaration int index = 0, c = 0, nodes_count = 0, d = info.height, maxtempx = -1, firstscan = 1, maxtempy = -1, mintempx = 502, mintempy = 502; is not yet large enough compared with declarations I have seen in questions of other beginners at SO.:) – Vlad from Moscow Jun 22 '23 at 21:31
  • @CodeBom if you use gcc, add '-fanalyzer -Wanalyzer-malloc-leak' to see leaks detected by static analyzer in diagrams at compile time. Another option (gcc and clang): either `-lasan` or `-fno-omit-frame-pointer -fsanitize=undefined -fsanitize=address`, and leak-sanitizer will display leaks at runtime (-g will add c-source lines caused leaks to that displaying) – yvs2014 Jun 22 '23 at 23:54
  • @Code Bom Can you post whole code? – Tom Jun 24 '23 at 09:47

0 Answers0