title: Receiving double free or corruption (top) when closing a pipe?
body: <p>So I have an array of n streams that I open with pipes, but using gdb, I found that the program fails when I try to close either the stream or the write end of the pipes.  I can write to the pipes just fine, but closing them doesn't work.  I ran valgrind on the program and all it did was print out </p>
  
  <pre><code>==4241== Warning: invalid file descriptor -1 in syscall read() 
  </code></pre>
  
  <p>over and over again.  Here is the code in question and error output.  If any more info is needed please let me know and I will try to provide it. Thanks everyone.  </p>
  
  <pre><code>191                 /*closes unused pipes and open streams to sorting processes*/
  192                 for (i = 0; i &lt; n; i++) {
  193                         close(parse2sortPipes[i][READ]);
  194                         close(sort2outputPipes[i][READ]);
  195                         close(sort2outputPipes[i][WRITE]);
  196                         outStreams[i] = fdopen(parse2sortPipes[i][WRITE], "w");
  197                 }
  198 
  199                 while (getWord(fstream, wBuf) != -1) {
  200                         if (i == n) {
  201                                 i = 0;
  202                         }
  203                         fputs(wBuf, outStreams[i]);
  204                         fputc('\n', outStreams[i]);
  205                         i++;
  206                 }
  207 
  208 
  209 
  210                 for (i = 0; i &lt; n; i++) {
  211                         fflush(outStreams[i]);
  212                         fclose(outStreams[i]); //this is where it fails
  213                         close(parse2sortPipes[i][WRITE]);
  214                 }
  215 
  
  
  *** Error in `..../xxx': double free or corruption (top): 0x0000000000603250 ***
  ======= Backtrace: =========
  208         for (i = 0; i &lt; n; i++) {
  /lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7ffff7a92a46]
  /lib/x86_64-linux-gnu/libc.so.6(fclose+0x14d)[0x7ffff7a80a6d]
  /home/user/school/cs/assignments/hw3/xxx[0x401477]
  /home/user/school/cs/assignments/hw3/xxx[0x400ce7]
  (gdb) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7ffff7a33ea5]
  /home/user/school/cs/assignments/hw3/xxx[0x400b39]
  ======= Memory map: ========
  00400000-00402000 r-xp 00000000 08:06 12588471                           /home/daniel/school/cs311/assignments/hw3/xxx
  00601000-00602000 r--p 00001000 08:06 12588471                           /home/daniel/school/cs311/assignments/hw3/xxx
  00602000-00603000 rw-p 00002000 08:06 12588471                           /home/daniel/school/cs311/assignments/hw3/xxx
  00603000-00624000 rw-p 00000000 00:00 0                                  [heap]
  7ffff77fc000-7ffff7810000 r-xp 00000000 08:06 3149442                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7ffff7810000-7ffff7a10000 ---p 00014000 08:06 3149442                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7ffff7a10000-7ffff7a11000 r--p 00014000 08:06 3149442                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7ffff7a11000-7ffff7a12000 rw-p 00015000 08:06 3149442                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7ffff7a12000-7ffff7bd0000 r-xp 00000000 08:06 3149417                    /lib/x86_64-linux-gnu/libc-2.17.so
  7ffff7bd0000-7ffff7dcf000 ---p 001be000 08:06 3149417                    /lib/x86_64-linux-gnu/libc-2.17.so
  7ffff7dcf000-7ffff7dd3000 r--p 001bd000 08:06 3149417                    /lib/x86_64-linux-gnu/libc-2.17.so
  7ffff7dd3000-7ffff7dd5000 rw-p 001c1000 08:06 3149417                    /lib/x86_64-linux-gnu/libc-2.17.so
  7ffff7dd5000-7ffff7dda000 rw-p 00000000 00:00 0 
  7ffff7dda000-7ffff7dfd000 r-xp 00000000 08:06 3149393                    /lib/x86_64-linux-gnu/ld-2.17.so
  7ffff7fd7000-7ffff7fda000 rw-p 00000000 00:00 0 
  7ffff7ff6000-7ffff7ff8000 rw-p 00000000 00:00 0 
  7ffff7ff8000-7ffff7ffa000 rw-p 00000000 00:00 0 
  7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
  7ffff7ffc000-7ffff7ffd000 r--p 00022000 08:06 3149393                    /lib/x86_64-linux-gnu/ld-2.17.so
  7ffff7ffd000-7ffff7fff000 rw-p 00023000 08:06 3149393                    /lib/x86_64-linux-gnu/ld-2.17.so
  7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  </code></pre>