gpt4 book ai didi

c++ - 尽管文件指针正确,但 fclose() 期间出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:11:24 24 4
gpt4 key购买 nike

  • 在对象销毁过程中始终发生;
  • fopen()之后和fclose()之前的文件指针值相同;
  • fclose() 在对象析构函数中只被调用一次;
  • 对象静态创建并在作用域结束时销毁
  • 文件只在一个线程中处理

打开文件:

logOutFile_fd = fopen(logOutFile,"w");
if (logOutFile_fd==NULL) {
/* omitted */
}

文件关闭:

if (logOutFile_fd!=NULL) {
fflush(logOutFile_fd);
fclose(logOutFile_fd);
}

我用“-g3”编译并运行了 gdb,这里是完整的回溯:

#0  0x00007ffff6f8d3f8 in raise () from /lib64/libc.so.6
No symbol table info available.
#1 0x00007ffff6f8effa in abort () from /lib64/libc.so.6
No symbol table info available.
#2 0x00007ffff6fcf6f9 in __libc_message () from /lib64/libc.so.6
No symbol table info available.
#3 0x00007ffff6fd7f4a in _int_free () from /lib64/libc.so.6
No symbol table info available.
#4 0x00007ffff6fdbc1c in free () from /lib64/libc.so.6
No symbol table info available.
#5 0x00007ffff6fc52c3 in fclose@@GLIBC_2.2.5 () from /lib64/libc.so.6
No symbol table info available.
#6 0x0000000000402733 in t_netInterface::~t_netInterface (this=0x7ffff5f50cf0, __in_chrg=<optimized out>) at connect.cpp:59
No locals.
#7 0x00000000004022fc in pthreadTask (pthData=0x7fffffffe070) at client.cpp:131
netInterface = {id = 162, currTime = {tv_sec = 0, tv_usec = 0}, dbg_time_sync = {tv_sec = 0, tv_usec = 0}, dbg_time_prev = 0, dbg_time_now = 0,
dbg_time_dprev = 0, dbg_time_dnow = 0, dbg_ch0_len = 0, datems = 0, timeMarkSet = {0 <repeats 12 times>}, chunk = 0, chunk_ch = 0 '\000',
chunk_dt = 0, chunk_H = 0 '\000', chunk_L = 0 '\000', fpd = 0x0, logOutFile = 0x0, logOutFile_fd = 0x7fffec038f30,
dataOutFile = 0x7fffec001c10 "(\b", dataOutFile_fd = 0x7fffec03a580, globalLogFile_fd = 0x7fffec0009d0, dir0 = 0x7fffec001d20 "(\b", sockfd = 11,
portno = 34000, serv_addr = {sin_family = 2, sin_port = 53380, sin_addr = {s_addr = 2717952192}, sin_zero = "\000\000\000\000\000\000\000"},
poll_d = {{fd = 11, events = 1, revents = 0}}, f_ret = 0, channelBuffers = 0x7fffec000920, channelBuffIter = 0x7fffec0008e0,
channelDataTypeH = 1284, valSigned = 0, dbgFlag = 0, dataPath = 0x7fffec006d50 "(\b", addr_str = 0x7fffec002530 "192.168.0.162",
buffChar = 0x7fffec002d40 "(\b", wasConnected = 0}
data = 0x7fffffffe070
out_buffer = 64
in_buffer = {0 <repeats 8192 times>}
pid = 162
tid = 162
logOutFile_fd = 0x7fffec038f30
res = 1
rcvDataSizeBytes = 0
retv = 0
cc = 2003
active = 0
uptimeSec = 27134
globalLogName = 0x7fffec0008c0 "x"
globalLogFile_fd = 0x7fffec0009d0
#8 0x00007ffff7328684 in start_thread () from /lib64/libpthread.so.0
No symbol table info available.
#9 0x00007ffff705eefd in clone () from /lib64/libc.so.6
No symbol table info available.

我做错了什么?提前致谢。

编辑: 我已经尝试调整解分配器,现在它看起来确实像是类构造函数和析构函数之间某处的内存垃圾。我将重新实现一些东西,所以这个问题可以被认为是封闭的。无论如何,感谢您的宝贵时间。

最佳答案

像这样的 fclose 崩溃表明所涉及的 FILE 数据结构已损坏,可能是由于程序中其他地方使用了无效指针。

最好的选择可能是内存检查工具,如 valgrind

关于c++ - 尽管文件指针正确,但 fclose() 期间出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48891012/

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