0

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)
0

1 Answer 1

0

I think the issue is that you're trying to free a part of a structure.

Since the data struct definition wasn't included, I'm assuming that n is defined as a char. If that's true, then the first free call is throwing the error because you can't free the 'n' part of a struct. Try commenting it out and instead, just free the entire structure. This should take care of it.

while (deletera!=fletter[i])
{
   node* deleterb=deletera;
   deletera=deletera->previous;
   free (deleterb);
}

However, if you're doing something different with the node struct and this doesn't work, then it would help if you'd add it to your post so we can see what's happening more clearly.

A simple way to remember - if something was allocated with malloc, it has to be free'd as a whole unit. You can't free just part of something that a malloc command created.

Hope this helps.

6
  • Ok, so when I comment out the freeing of n in the struct I free roughly 143 out of the 286 allocs, roughly half. The struct definition has three element 1. const char* n which is linked to a buffer with a word from the dictionary. 2. struct node* next; 3. struct node* previous; } Apr 10, 2015 at 19:21
  • I think at this point, we need to see how the dictionary is created. It seems like something is being overlooked, probably where the words themselves are being stored.
    – Cliff B
    Apr 10, 2015 at 19:33
  • Ok I solved it! I just had to change const char n to char* in the struct definition. I still have 59 leaks though. Do you know how I can get Valgrind to give me the details on these leaks? Apr 10, 2015 at 19:41
  • Sorry, my crystal ball is broken today. ;-) But if you could post the valgrind output and maybe any related code? Take a run at it yourself first, looking at all of your malloc code. If you can't figure it out, maybe it's time for a new question? Also, if you consider this part of the problem answered, please mark it as answered. (housekeeping on the forum.)
    – Cliff B
    Apr 10, 2015 at 20:06
  • Certainly no problem! Thanks for your help! Apr 10, 2015 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .