title: Pointer being freed was not allocated, Abort trap: 6
body: <p>I'm not proficient in C programming so please excuse me if this isn't a strong question. In the following code, I can only allocate memory to <code>samplesVec</code>after obtaining the value of <code>nsamplepts</code>, but I need to return the vector <code>samplesVec</code> to the <code>main</code> for further use (not yet coded). However, I'm getting the following error:</p>
  
  <p><strong>Error in Terminal Window</strong>: 
  ImportSweeps(3497,0x7fff7b129310) malloc: <strong>* error for object 0x7fdaa0c03af8: pointer being freed was not allocated
  *</strong> set a breakpoint in malloc_error_break to debug
  Abort trap: 6</p>
  
  <p>I'm using Mac OS X Mavericks with the gcc compiler. Thanks for any help.</p>
  
  <p>*<em>EDITED!!! AFTER VALUABLE INPUTS FROM COMMENTATORS, THE FOLLOWING REPRESENTS A SOLUTION TO THE ORIGINAL PROBLEM (WHICH IS NO LONGER AVAILABLE) *</em></p>
  
  <p>The following code modification seemed to solve my original questions. Thanks for the valuable inputs everyone!</p>
  
  <pre><code>/* Header Files */
  #define LIBAIFF_NOCOMPAT 1 // do not use LibAiff 2 API compatibility
  #include &lt;libaiff/libaiff.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  /* Function Declarations */
  void FileSearch(char*, char*, char*, char*, char*);
  int32_t *ImportSweeps(char*);
  
  /* Main */
  int main()
  {
  char flag1[2] = "N";
  char binname[20] = "bin1"; // dummy assignment
  char buildfilename[40] = "SweepR";
  char skeletonpath[100] = "/Users/.../Folder name/";
  int k, len;
  
  /* Find the sweep to be imported in the directory given by filepath */
  FileSearch(skeletonpath, binname, buildfilename, skeletonpath, flag1);
  if (strcmp(flag1,"Y")) {
      printf("No file found. End of program.\n");
  } else {
      len = (int) strlen(skeletonpath);
      char *filepath = malloc(len);
      for (k = 0; k &lt; len; k++) {
          filepath[k] = skeletonpath[k];
      }
      printf("File found! Filepath: %s\n", filepath);
      // Proceed to import sweep
      int32_t *sweepRfile = ImportSweeps(filepath);
      if (sweepRfile) {
          printf("Success!\n");
          // Do other things with sweepRfile
          free(sweepRfile);
      }
      free(filepath);
  }
  return 0;
  }
  
  /* Sub-Routines */
  void FileSearch(char *dir, char *binname, char *buildfilename, char* filepath, char* flag1)
  {
  DIR *dp;
  struct dirent *entry;
  struct stat statbuf;
  if((dp = opendir(dir)) == NULL) {
      fprintf(stderr,"Cannot open directory: %s\n", dir);
      return;
  }
  chdir(dir);
  while((entry = readdir(dp)) != NULL) {
      lstat(entry-&gt;d_name, &amp;statbuf);
      if(S_ISDIR(statbuf.st_mode)) {
          /* Found a directory, but ignore . and .. */
          if(strcmp(".",entry-&gt;d_name) == 0 || strcmp("..",entry-&gt;d_name) == 0)
              continue;
          strcpy(binname,entry-&gt;d_name);
          strcpy(buildfilename,"SweepR");
          /* Recurse at a new indent level */
          FileSearch(entry-&gt;d_name, binname, buildfilename, filepath, flag1);
      }
      else {
          sprintf(buildfilename, "%s%s.aiff", buildfilename, binname);
          if (strcmp(entry-&gt;d_name,buildfilename)) {
              strcpy(buildfilename,"SweepR");
          } else {
              sprintf(filepath, "%s%s/%s", filepath, binname, buildfilename);
              strcpy(flag1,"Y");
              break;
          }
      }
  }
  chdir("..");
  closedir(dp);
  }
  
  
  int32_t *ImportSweeps(char *filepath)
  {
  char *filepathread = filepath;
  
  /* Initialize files for importing */
  AIFF_Ref fileref;
  
  /* Intialize files for getting information about AIFF file */
  uint64_t nSamples;
  int32_t *samples = NULL;
  int32_t *samplesVec = NULL;
  int channels, bitsPerSample, segmentSize, ghost, nsamplepts;
  double samplingRate;
  
  /* Import Routine */
  fileref = AIFF_OpenFile(filepathread, F_RDONLY) ;
  if(fileref)
  {
      // File opened successfully. Proceed.
      ghost = AIFF_GetAudioFormat(fileref, &amp;nSamples, &amp;channels, &amp;samplingRate, &amp;bitsPerSample, &amp;segmentSize);
      if (ghost &lt; 1)
      {
          printf("Error getting audio format.\n");
          AIFF_CloseFile(fileref); return (int32_t) 0;
      }
      nsamplepts = ((int) nSamples)*channels;
      samples = malloc(nsamplepts * sizeof(int32_t));
      samplesVec = malloc(nsamplepts * sizeof(int32_t));
      ghost = AIFF_ReadSamples32Bit(fileref, samples, nsamplepts);
      if (ghost) {
          for (int k = 0; k &lt; nsamplepts; k++) {
              samplesVec[k] = *(samples+k);
          }
      }
      free(samples);
      AIFF_CloseFile(fileref);
  }
  return samplesVec;
  }
  </code></pre>