Clearing a double linked list Trie structure in c

I want to prevent a memory leak, so I want to free the trie. below you can see my attempt to free up used memory.

// to see how many words are cleaned up.
static int teller_cleanup = 0;

struct ac {
    int value;
    char character; 
    char * word;
    struct ac *next;
    struct ac *previous;
    struct ac *child;
    struct ac *parent;
};

this is a double or 4-linked list, not sure that, judging by his words, I

void cleaner(struct ac* a) {
    ac * temp = NULL;
    if (a != NULL) {
        if (a -> child == NULL && a -> next == NULL) {
            teller_cleanup ++;
            if (a -> parent != NULL) {
                temp = a -> parent;
            }
            else {
                temp = a -> previous;
             }
             free(a -> word);
             free(a);
             a = temp;
        }
        if (a -> child != NULL) {
            cleaner(a -> child);
        }
        if (a -> next != NULL) {
            cleaner(a -> next);
        }
     }
 }

int cleanup(struct ac* a) {
    // means that it is in the root
    // therfore it needs to go to the first node.
    if (a -> next == NULL && a -> parent == NULL) {
        a = a -> child;
    }
    cleaner(a);
    return teller_cleanup;
}

But it looks like it is not working properly. this gives an error:

double free or corrupt (fasttop): 0x0000000000fffa70 ***

what I don't seem to get, because when "child" and "next" are "NULL" than "a", this is the outper most node. And I believe that only one of the recusive if arguments can go to one of these outher most nodes.

I will try to imagine trie:

[root]
   |
  \/
[h] -- > [b]
 |        |
\/       \/
[i]      [y] --> [e] 

trie hi, by be. , . 'h' 'b' , 'h' 'i' .

- , ? .

+4
1

, , NULL . , NULL , .

, teller_cleanup, cleaner().

void cleaner(struct ac *a, int *teller_cleanup) 
{
    if (a != NULL) {
        cleaner(a->next, teller_cleanup);
        cleaner(a->child, teller_cleanup);
        free(a->word);
        free(a);
        (*teller_cleanup)++;
    }
}

int cleanup(struct ac *a)
{
    int teller_cleanup = 0;
    cleaner(a, &teller_cleanup);
    return teller_cleanup;
}
+2

All Articles