title: malloc error: incorrect checksum for freed object in struct for reading file
body: <p>So I have this function:</p>
  
  
  
  <pre><code>source_t * source_init(char * argsource)
  {
      source_t * temp = (source_t *) malloc(sizeof(source_t *));
      temp-&gt;path = realpath(argsource, NULL);
      temp-&gt;dir = dirname(temp-&gt;path);
      temp-&gt;name = basename(temp-&gt;path);
      temp-&gt;ext = strrchr(temp-&gt;name, '.');
      temp-&gt;content = read_file(temp-&gt;path); // here
      temp-&gt;error = error_free();
      return temp;
  }
  </code></pre>
  
  <p>It's calling function <code>read_file()</code>:</p>
  
  
  
  <pre><code>char * read_file(char * sourcepath)
  {
      char * buffer = NULL;
      long string_size, read_size;
      FILE * file = fopen(sourcepath, "r");
      fseek(file, 0, SEEK_END);
      string_size = ftell(file);
      rewind(file);
      buffer = (char *) malloc(sizeof(char) * (string_size + 1) );
      read_size = fread(buffer, sizeof(char), string_size, file);
      buffer[string_size] = '\0';
      if (string_size != read_size)
      {
          free(buffer);
          buffer = NULL;
      }
      fclose(file);
      return buffer;
  }
  </code></pre>
  
  <p>and this error comes up: <code>malloc: *** error for object 0x7faf08402698: incorrect checksum for freed object - object was probably modified after being freed.</code> So my current solution is to separately initialise <code>content</code> after I have called <code>source_init()</code> in the main function. Although that solution works, I want <code>content</code> to be initialised in <code>source_init()</code>. Also it seems I can't directly call <code>source_init()</code> to initialise <code>content</code> because the same error comes up so I have to create a buffer to call <code>source_init()</code> and initialise <code>content</code> to buffer.</p>