title: Error in 'a.out': free(): invalid next size (normal)
body: <p>I'm writing a big application and i recently included there my new module for working with files. It has simple functions of writing integers and strings, it was tested to be perfect on Red Hat. It does exactly what i want.</p>
  
  <p>Here's the implementation of the functions there:</p>
  
  
  
  <pre><code>int CFile_open(CFile* owner, char* filename, char* mode) {
      owner-&gt;file = fopen(filename, mode);
      if (owner-&gt;file == NULL) {
          return 0;
      } else {
          return 1;
      }
  }
  
  int CFile_close(CFile* owner) {
      if (owner-&gt;file == NULL) {
          return 0;
      } else {
          if (fclose(owner-&gt;file) ) {
              return 1;
          } else {
              return 0;
          }
      }
  }
  
  int CFile_writeInt(CFile* owner, int num) {
      if (owner-&gt;file == NULL) {
          return 0;
      } else {
          if (fwrite(&amp;num, sizeof(int), 1, owner-&gt;file)) {
              return 1;
          } else {
              return 0;
          }
      }
  }
  
  int CFile_writeString(CFile* owner, char* str) {
      if (owner-&gt;file == NULL) {
          return 0;
      } else {
          char b = strlen(str);
          if (fwrite(&amp;b, sizeof(char), 1, owner-&gt;file)) {
              for (int i=0; i&lt;=b; i++) {
                  fwrite(&amp;str[i], sizeof(char), 1, owner-&gt;file);
              }
          } else {
              return 0;
          }
      }
  }
  </code></pre>
  
  <p>These functions are being used in the wrapper-function and then being executed in my application.<br>
  Here's that function:</p>
  
  
  
  <pre><code>void fileWrite(void* data) {
      CList* list = (CList*)data;
      CNode* node = list-&gt;first;
      CData* info = NULL;
      CFile* f = (CFile*)malloc(sizeof(CFile));
      f-&gt;init = CFile_init;
      f-&gt;init(f);
  
      if (list-&gt;length == 0) {
          printf("%s\n", "The list is empty!");
          wait();
      } else {
          int i = 1;
  
          if (f-&gt;open(f, (char*)"./f", (char*)"w")) {
              while (node) {
                  info = node-&gt;getData(node);
  
                  if (f-&gt;writeInt(f, info-&gt;getNum(info))) {
                      if (f-&gt;writeString(f, info-&gt;getAdr(info))) {
                          if (f-&gt;writeString(f, info-&gt;getPh(info))) {
                              printf("%s%d\n", "Wrote item #", i);
                              i++;
                          } else {
                              printf("%s%d\n", "Error writing Phone of item #", i);
                          }
                      } else {
                          printf("%s%d\n", "Error writing Adress of item #", i);
                      }
                  } else {
                      printf("%s%d\n", "Error writing Number of item #", i);
                  }
  
                  node = node-&gt;next;
              }
              if (f-&gt;close(f)) {
                  printf("%s\n", "Wrote successfully");
                  wait();
              }
          }
          free(f);
      }
  }
  </code></pre>
  
  <p>The problematic spot is the <code>f-&gt;close(f)</code> at the very end of the function.<br>
  If i don't close the file, everything works flawlessly. But if i do, i'm getting the following error:</p>
  
  <pre><code>*** Error in 'a.out': free(): invalid next size (normal): 0x0859c320 ***
  </code></pre>
  
  <p>However, despite the application crash, it does close the file, and the info i wrote to it is correct and not corrupted.</p>
  
  <p>But i can't figure out why am i getting the error. I close the file only once (as you may see from the function), and i also tried commenting the <code>free(f)</code> line.<br>
  Also if i call <code>fclose(f-&gt;file)</code> there i get the same error, so that's not the <code>close()</code> function issue.</p>
  
  <p>I assume that the rest of the application works (and worked before i added the function) perfectly except for the line where i close the file.</p>
  
  <p>The reason i mentioned RedHat is because it was working perfectly there, but when i recompiled it under ArchLinux (also tried Debian) - it won't close the file. Same compiler versions were used everywhere.</p>
  
  <p>I compile it with <code>g++</code> if that matters, but i also have the same issue with <code>clang++</code>.</p>
  
  <p>I also tried running it with <code>valgrind</code> and it says nothing about files, or i just couldn't find the right keys for that. Perhaps you know?</p>
  
  <p>So do you have any ideas on what's going on and how to fix it? I was staring at the code for literally 3 hours already and i can't figure it out for the life of me.</p>
  
  <hr>
  
  <p>Test case:</p>
  
  <p><code>info-&gt;getNum()</code> returns integer "11" (assigned during input)<br>
  <code>info-&gt;getAdr()</code> returns dynamically allocated char array "aa" (allocated and assigned during input)<br>
  <code>info-&gt;getPh()</code> returns dynamically allocated char array "bb" (allocated and assigned during input)</p>
  
  <p>I can test these values using other functions in my application, so i'm 100% confident they are correct.</p>
  
  <p>Then i just evoke the <code>fileWrite()</code> function.</p>