Home > Software engineering >  C Memory leaks to free for a Linked List
C Memory leaks to free for a Linked List

Time:12-19

When I valgrind, I have memory leaks and errors (errors are in another test file) in my main.c. (screen at the end) Also, I have to free only in my function list_destroy in list.c !
Check this out :

EDIT : thanks to @kirjosieppo I removed a malloc in create_cell function below ! Now : valgrind_updated

cell.c

// cell_s has gpointer ptr_value and struct cell_s *next
cell_t* create_cell(gpointer v) {
    cell_t *c = malloc(sizeof(cell_t));
    c->next = NULL;
    c->ptr_value = v;

    return c;
}

void destroy_int(gpointer data) {
    free((int *) data);
}

list.c

// list_s has cell_t *head and int size
list_t* list_create() {
    list_t *l = malloc(sizeof(list_t));

    l->head = NULL;
    l->size = 0;

    return l;
}

void list_insert_in_head(list_t *l, gpointer element) {
// typedef cell_t* adr
    adr address_c = create_cell(element);

    address_c->next = l->head;
    l->head = address_c;

      l->size;
}

void list_insert_next(list_t *l, gpointer element, adr address) {
    adr address_c = create_cell(element);

    if (l->head == NULL) {
        liste_insert_in_head(l, element);
    } else {
        address_c->next = address->next;
        address->next = address_c;
    }

      l->size;
} 

void list_remove_in_head(list_t *l) {
    if (l->head != NULL) {
        l->head = l->head->next;
    }

    --l->size;
}

void list_remove_after(list_t *l, adr address) {
    if (l->head->next == NULL) {
        printf("Use list_remove_in_head function\n");
    } else if (address != NULL) {
        address->next = address->next->next;

        --l->size;
    }
}

// Here is the problem !
void list_destroy(list_t *l, list_gfree ft_destroy) {
    adr current = l->head;

    while(current != NULL) {
        adr tmp = current;

        current = current->next;
        
        ft_destroy(tmp->ptr_value);
        tmp->ptr_value = NULL;
        
        ft_destroy(tmp);
    }

    free(l);
}

main.c

int main(void) {
    list_t *l = list_create();

    int *ptr_int = (int *)malloc(sizeof(int));
    *ptr_int = 4;
    list_insert_in_head(l, ptr_int);
    printf("Size : %d\n", l->size);

    int *ptr_int_2 = (int *)malloc(sizeof(int));
    *ptr_int_2 = 7;
    list_insert_in_head(l, ptr_int_2);
    printf("Size : %d\n", l->size);

    int *ptr_int_3 = (int *)malloc(sizeof(int));
    *ptr_int_3 = 100;
    list_insert_next(l, ptr_int_3, l->head);
    printf("Size : %d\n", l->size);

    list_remove_in_head(l);
    printf("Size : %d\n", l->size);

    list_remove_next(l, l->head);
    printf("Size : %d\n", l->size);

    list_remove_next(l, l->size);
    printf("Size : %d\n", l->size);

    list_destroy(l, destroy_int);
}

valgrind
Memory leaks are detected in my insertions. valgrind

Thanks for helping me to have 0 memory leaks and 0 errors ! :-)

CodePudding user response:

I haven’t yet tested if that really is the (only) issue, but there’s a code like this:

create_cell(gpointer v) {
// clip
    c->ptr_value = malloc(sizeof(int));
    c->ptr_value = v;

An allocation is made and then the pointer is thrown away.

Let’s continue! What else is there to be found?

In list_remove_in_head() the cell l->head is written over and not freed. It can be fixed with something like this:

void list_remove_in_head(list_t *l) {
    if (l->head != NULL) {
        void *tmp = l->head->next;
        free(l->head->ptr_value);
        free(l->head);
        l->head = tmp;
    }

    --l->size;
}

The same applies to list_remove_after(), too, but I suppose you can work it out yourself. :)

  • Related