gpt4 book ai didi

c++ - 关闭管道时收到双重释放或损坏(顶部)?

转载 作者:行者123 更新时间:2023-11-30 17:44:36 30 4
gpt4 key购买 nike

所以我有一个用管道打开的 n 个流的数组,但是使用 gdb,我发现当我尝试关闭流或管道的写入端时程序失败。我可以很好地写入管道,但关闭它们不起作用。我在程序上运行 valgrind,它所做的只是打印出来

==4241== Warning: invalid file descriptor -1 in syscall read() 

一遍又一遍。这是有问题的代码和错误输出。如果需要更多信息,请告诉我,我会尽力提供。感谢大家。

191                 /*closes unused pipes and open streams to sorting processes*/
192 for (i = 0; i < 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 < 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 < 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]

最佳答案

fclose 将关闭底层文件描述符,因此之后不要调用 close

关于c++ - 关闭管道时收到双重释放或损坏(顶部)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19920765/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com