I've been working on speller for about two weeks now and I've finally got the programm to work, but valgrind cam up with a few memory errors. I've managed to solve most of them on my own, but the leak in my unload function still eludes me. I will obviously keep trying on my own, but I would love it if someone could take a look with fresh eyes and tell me what I'm missing.
The valgrind error is:
56 bytes in 1 blocks are definitely lost in loss record 1 of 1: (file: dictionary.c, line: 127)
and this is my unload function:
// Unloads dictionary from memory, returning true if successful else false
bool unload(void) //Line 115
{
//Declarations
int freed = 0;
node *cursor = malloc(sizeof(node));
if (cursor == NULL) //Line 120
{
printf("Memory Error: Unloading.");
free(cursor);
return false;
} //Line 125
node *tmp = malloc(sizeof(node)); //Line 127!!!!!!!!!!!!!!!!!
if (tmp == NULL)
{
printf("Memory Error: Unloading."); //Line 130
free(tmp);
return false;
}
//Line 135
//Function
for (int i = N; i > 0; i--)
{
cursor->next = table[i];
while(cursor->next != NULL) //Line 140
{
tmp = cursor;
cursor = cursor->next;
free(tmp);
} //Line 145
freed++;
}
free(cursor);
if (freed == N)
{
return true;
}
else
{
return false;
}
}