So I am having trouble figuring out how to get rid of this memory leak. The leak is related (I believe) to the const char *'s in the n value of my nodes in the linked list.
Here is the valgrind diagnostic:
in use at exit: 6,582,968 bytes in 143,125 blocks
==6635== total heap usage: 286,217 allocs, 143,092 frees, 8,300,412 bytes
allocated
LEAK SUMMARY:
==6635== definitely lost: 6,582,570 bytes in 143,123 blocks
==6635== indirectly lost: 0 bytes in 0 blocks
==6635== possibly lost: 46 bytes in 1 blocks
==6635== still reachable: 352 bytes in 1 blocks
==6635== suppressed: 0 bytes in 0 blocks
==6635== Rerun with --leak-check=full to see details of leaked memory
==6635==
Here is my code
bool unload(void)
{
for (int i=0;i<26;i++)
// go through dictionary until you hit NULL
{
node* deletera= fletter[i];
while (deletera->next!=NULL)
{
deletera=deletera->next;
}
//go to previous
while (deletera!=fletter[i])
{
node* deleterb=deletera;
deletera=deletera->previous;
//const char* word=&(deleterb->n); **//I don't no how to free this//**
//free(word);
free (deleterb);
}
}
for (int i=0;i<26;i++)
{
free(fletter[i]);
}
//delete
return true;
}
when I try this
node* deleterb=deletera;
deletera=deletera->previous;
free(deleterb->n);
free (deleterb);
I get the following error
dictionary.c:207:13: error: passing 'const char *' to parameter of type 'void *'
discards qualifiers
[-Werror,-Wincompatible-pointer-types-discards-qualifiers]
free(deleterb->n);
when I try
{
node* deleterb=deletera;
deletera=deletera->previous;
free(&deleterb->n);
free (deleterb);
}
}
I just get a ton of valgrind errors.
Invalid free() / delete / delete[] / realloc()
==8435== at 0x4007B21: free (in /usr/lib/valgrind/vgpreload_memcheck-x86- linux.so)
==8435== by 0x80492A1: unload (dictionary.c:208)
==8435== by 0x8048C82: main (speller.c:160)
==8435== Address 0x4025898 is 0 bytes inside a block of size 12 free'd
==8435== at 0x4007B21: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8435== by 0x8049296: unload (dictionary.c:207)
==8435== by 0x8048C82: main (speller.c:160)