title: How do I fix these memory leaks in C?
body: <p>I am working on a function which allocates memory into an array, and then I free it later. Oddly enough, valgrind is giving me this error:</p>
  <pre><code>==93== HEAP SUMMARY:
  ==93==     in use at exit: 11,160 bytes in 2,232 blocks
  ==93==   total heap usage: 44,310 allocs, 42,078 frees, 1,632,230 bytes allocated
  ==93==
  ==93== 11,160 bytes in 2,232 blocks are definitely lost in loss record 1 of 1
  ==93==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==93==    by 0x10976F: FillHashMap (in /mnt/c/Users/Jordan/Documents/GitHub/flwg/flwp)
  ==93==    by 0x1092F1: main (in /mnt/c/Users/Jordan/Documents/GitHub/flwg/flwp)
  ==93==
  ==93== LEAK SUMMARY:
  ==93==    definitely lost: 11,160 bytes in 2,232 blocks
  ==93==    indirectly lost: 0 bytes in 0 blocks
  ==93==      possibly lost: 0 bytes in 0 blocks
  ==93==    still reachable: 0 bytes in 0 blocks
  ==93==         suppressed: 0 bytes in 0 blocks
  ==93==
  ==93== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
  </code></pre>
  <p>Dr. Memory is giving me a similar error:</p>
  <pre><code>Error #1: UNADDRESSABLE ACCESS: reading 0x0000000000000000-0x0000000000000008 8 byte(s)
  # 0 testing               [C:/Users/Jordan/Documents/GitHub/flwg/FLWP-2.c:120]
  # 1 main                  [C:/Users/Jordan/Documents/GitHub/flwg/FLWP-2.c:105]
  Note: @0:00:01.195 in thread 14704
  Note: instruction: mov    (%rax) -&gt; %rax
  </code></pre>
  <p>The strcmp line of code is what causes it to break with Dr. Memory, although valgrind says I have issues even if I don't call this particular method.</p>
  <pre><code>void testing(struct wordConnections *header){
      header = header-&gt;nextRow;
      
      while(strcmp(header-&gt;word, &quot;berk&quot;) != 0){ 
          header = header-&gt;nextRow; 
          if(header == NULL){
           printf(&quot;Failed&quot;);
           return; 
          }
      }
  }
  </code></pre>
  <p>The code that runs is (the value and significance of numLetters + 1 is it is the length of the char* stored in the wordStorage, the value of numLetters is 4):</p>
  <pre><code>char** FillHashMap(struct wordConnections **(*HashMap)){
      int h = 0; 
      /* The Amount of words in each file, File 1, 2, 3 */
      int totalWordQuantity[3] = {132, 7420, 19829}; 
      /*the word that we test, we add by two because first 4: word, 5th: \n, 6th: \0*/
      char word[numLetters + 1]; 
      /*how many times we've changed the character*/
      int letterSpot = 0; 
      /*the character that goes through the file*/
      char c; 
      FILE *flwd = OpenFile(); 
      /* is it the first word of the line */
      int wordCount = 0; 
      int isFirst = 1; 
      char p = fgetc(flwd); 
      c = p; 
      /* So, this is a temporary word, who will store the row, so all of its category folks will be contained within it */
      char* rowWord = malloc(sizeof(char) * (numLetters + 1)); 
      /*This stores all of the words*/
      char** wordStorage = (char**)calloc(totalWordQuantity[numLetters - 2], sizeof(char*) * (numLetters + 1));
      int i = 0; 
      /* First, take the character */
      while((c = p) != EOF){
          
          p = fgetc(flwd);
  
          /* Add the character to the word */
          word[letterSpot] = c; 
          /* Allows the letter spot to find the next place into the word */
          letterSpot++; 
          
          /* Determines if the character is the placement immediately after the word */
          if((c == ' ' &amp;&amp; p != '\n') || c == '\n'){
              
              letterSpot = 0; 
              /* Throws in the \0, converting into a string */
              word[numLetters] = '\0'; 
              
              wordStorage[wordCount] = malloc(sizeof(char) * (numLetters + 1));
              h++;
              strcpy(wordStorage[wordCount], word); 
                  
              /* Determine if it's the first word */
              if(isFirst == 1){
                  strcpy(rowWord, word); 
                   
                  /* Throw it in as a row */
                  AddRow2DLL(wordStorage[wordCount], 
                           HashMap[FirstHashFunction(word[0])/*First Letter*/]
                                  [SecondHashFunction(word)]/*First Vowel*/);     
              }
              /* If it's not the first word */
              else {
                  AddColumn2DLL(wordStorage[wordCount], 
                         HashMap[FirstHashFunction(rowWord[0])/*First Letter*/]
                                [SecondHashFunction(rowWord)]/*First Vowel*/); 
              }
              
              if(c == ' '){
                  isFirst = 0; 
              }
              if(c == '\n'){
                  isFirst = 1; 
              }
              wordCount++; 
          }
          c = p; 
      }
      free(rowWord);
      fclose(flwd); 
      return wordStorage; 
  }
  </code></pre>
  <p>The hash map works fine, this is the method that causes the issues, without it, there are no memory leaks.</p>
  <p>Later in the code, I free the wordStorage array with this method:</p>
  <pre><code>void FreeWordStorage(char** wordStorage){
      int f = 0;  
      /* The Amount of words in each file, File 1, 2, 3 */
      int totalWordQuantity[3] = {132, 7420, 19829}; 
      int i; 
      for(i = 0; i &lt; totalWordQuantity[numLetters - 2]; i++){
          free(wordStorage[i]); 
          f++; 
      }
       
      free(wordStorage);
  }
  </code></pre>
  <p>I've tried all sorts of things to fix it. Oddly enough, when I check it with integers, I'm allocating and freeing the same amount of data.</p>
  <p>Any help would be greatly appreciated. Thank you!</p>