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

时间:2018-02-20 17:28:39

标签: c++ linux gcc memory segmentation-fault

  • 在物体破坏期间始终发生;
  • fopen()之后和fclose()之前的文件指针值相同;
  • fclose()在对象析构函数中只被调用一次;
  • 对象在范围的末尾静态创建并销毁
  • 该文件仅在一个线程中处理

文件打开:

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

文件关闭:

#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.

我使用&#34; -g3&#34;并通过gdb运行,​​这是完整的回溯:

{{1}}

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

编辑我试图改变deallocators,现在它看起来像是在类构造函数和析构函数之间的内存。我将重新实现一些事情,因此这个问题可以被认为是封闭的。不管怎样,谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

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

你最好的选择可能是像valgrind

这样的记忆检查工具