title: Invalid Read of size 8 and other things
body: <p>I am new to C and I seem to be having so issues implementing a linked list. The List works, what doesn't work is the memory management. I am not the greatest at it but I seem to be doing some things wrong.</p>
  
  <p>I a receiving an invalid read of size 8 from valgrind. The Exact error is as follows:</p>
  
  <pre><code>==2155== Invalid read of size 8
  ==2155==    at 0x401271: DestroyLinkedList (in /home/btm7984/hw3/TestList)
  ==2155==    by 0x400EFE: main (TestList.c:92)
  ==2155==  Address 0x8788218 is 8 bytes inside a block of size 24 free'd
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
  ==2155==    by 0x400EB3: main (TestList.c:87)
  ==2155==
  ==2155== Invalid read of size 8
  ==2155==    at 0x401242: DestroyLinkedList (in /home/btm7984/hw3/TestList)
  ==2155==    by 0x400EFE: main (TestList.c:92)
  ==2155==  Address 0x8788218 is 8 bytes inside a block of size 24 free'd
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
  ==2155==    by 0x400EB3: main (TestList.c:87)
  ==2155==
  ==2155== Invalid read of size 8
  ==2155==    at 0x40124E: DestroyLinkedList (in /home/btm7984/hw3/TestList)
  ==2155==    by 0x400EFE: main (TestList.c:92)
  ==2155==  Address 0x8788210 is 0 bytes inside a block of size 24 free'd
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
  ==2155==    by 0x400EB3: main (TestList.c:87)
  ==2155==
  ==2155== Invalid free() / delete / delete[] / realloc()
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401258: DestroyLinkedList (in /home/btm7984/hw3/TestList)
  ==2155==    by 0x400EFE: main (TestList.c:92)
  ==2155==  Address 0x8788160 is 0 bytes inside a block of size 104 free'd
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x400EE0: main (TestList.c:89)
  ==2155==
  ==2155== Invalid free() / delete / delete[] / realloc()
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401264: DestroyLinkedList (in /home/btm7984/hw3/TestList)
  ==2155==    by 0x400EFE: main (TestList.c:92)
  ==2155==  Address 0x8788210 is 0 bytes inside a block of size 24 free'd
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
  ==2155==    by 0x400EB3: main (TestList.c:87)
  ==2155==
  ==2155== Invalid free() / delete / delete[] / realloc()
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x400F1C: main (TestList.c:94)
  ==2155==  Address 0x8787dd0 is 0 bytes inside a block of size 24 free'd
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401264: DestroyLinkedList (in /home/btm7984/hw3/TestList)
  ==2155==    by 0x400EFE: main (TestList.c:92)
  ==2155==
  ==2155== Invalid free() / delete / delete[] / realloc()
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x400F2B: main (TestList.c:95)
  ==2155==  Address 0x8787dd0 is 0 bytes inside a block of size 24 free'd
  ==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
  ==2155==    by 0x401264: DestroyLinkedList (in /home/btm7984/hw3/TestList)
  ==2155==    by 0x400EFE: main (TestList.c:92)
  ==2155==
  ==2155==
  ==2155== HEAP SUMMARY:
  ==2155==     in use at exit: 24 bytes in 1 blocks
  ==2155==   total heap usage: 413,183 allocs, 413,188 frees, 26,444,136 bytes allo cated
  ==2155==
  ==2155== LEAK SUMMARY:
  ==2155==    definitely lost: 24 bytes in 1 blocks
  ==2155==    indirectly lost: 0 bytes in 0 blocks
  ==2155==      possibly lost: 0 bytes in 0 blocks
  ==2155==    still reachable: 0 bytes in 0 blocks
  ==2155==         suppressed: 0 bytes in 0 blocks
  ==2155== Rerun with --leak-check=full to see details of leaked memory
  ==2155==
  ==2155== For counts of detected and suppressed errors, rerun with: -v
  ==2155== ERROR SUMMARY: 13 errors from 7 contexts (suppressed: 2 from 2)
  </code></pre>
  
  <p>This is comming from the following code:</p>
  
  <pre><code>   FILE *fp;
     char tmpString[100];
     LinkedLists *ListPtr = malloc(sizeof(LinkedLists));
     ElementStructs *DataPtr;
     LinkedListNodes* curr = malloc(sizeof(LinkedListNodes));
     int counter = 0;
     int Done = 0;
     if (argc ==2){
     InitLinkedList(ListPtr);
     fp = fopen(argv[1], "r");
     if (!fp){
       fprintf(stderr,"%s Cannot open file %s\n", argv[0], argv[1]);
       exit(1);
     }
     do{
       fscanf(fp,"%s",tmpString);
       if (!feof(fp)) {
         DataPtr = malloc(sizeof(ElementStructs));
         strcpy(DataPtr-&gt;str,tmpString);
         DataPtr-&gt;index=counter;
         AddToBackOfLinkedList(ListPtr, DataPtr);
         counter++;
         Done = 1;
       } else {
         Done = 0;
       }
     }while (Done);
  
     printf("The first 6 elements of the LinkedList:\n");
     curr = ListPtr-&gt;FrontPtr;
     printf("Element %d is %s\n",curr-&gt;ElementPtr-&gt;index,curr-&gt;ElementPtr-&gt;str);
     curr = curr-&gt;Next;
     printf("Element %d is %s\n",curr-&gt;ElementPtr-&gt;index,curr-&gt;ElementPtr-&gt;str);
     curr = curr-&gt;Next;
     printf("Element %d is %s\n",curr-&gt;ElementPtr-&gt;index,curr-&gt;ElementPtr-&gt;str);
     curr = curr-&gt;Next;
     printf("Element %d is %s\n",curr-&gt;ElementPtr-&gt;index,curr-&gt;ElementPtr-&gt;str);
     curr = curr-&gt;Next;
     printf("Element %d is %s\n",curr-&gt;ElementPtr-&gt;index,curr-&gt;ElementPtr-&gt;str);
     curr = curr-&gt;Next;
     printf("Element %d is %s\n\n",curr-&gt;ElementPtr-&gt;index,curr-&gt;ElementPtr-&gt;str);
  
  
     fclose(fp);
     DestroyLinkedList(ListPtr);
     free(ListPtr);
     free(curr);
     free(curr);
     exit(0);
     return 0;
     } else {
       fprintf(stderr,"Usage: %s &lt;inputfile&gt;", argv[0]);
       return 2;
     }
  </code></pre>
  
  <p>In a separate Module I have some of these functions defined Noteably, DestroyLinkedLists:</p>
  
  <pre><code>void DestroyLinkedList(LinkedLists *ListPtr){
     LinkedListNodes* temp = ListPtr-&gt;FrontPtr;
     LinkedListNodes* nextTemp = ListPtr-&gt;FrontPtr;
     for (;temp-&gt;Next!=NULL;){
       nextTemp = temp-&gt;Next;
       free (temp-&gt;ElementPtr);
       free (temp);
       temp = nextTemp;
     }ListPtr-&gt;NumElements=0;
     ListPtr-&gt;FrontPtr=NULL;
     ListPtr-&gt;BackPtr=NULL;
  }
  </code></pre>
  
  <p>Any help in figuring out these memory errors would be greatly appreciated.</p>