c++ - 关闭二进制文件时出现段错误

标签 c++ segmentation-fault binaryfiles

下面给出了导致段错误的代码部分。

ifstream xiFileId(xifile, ios::binary); //xifile is a char * 

//the ii_t class in the following line is taken from http://stackoverflow.com/questions/1855704/c-binary-file-i-o-to-from-containers-other-than-char-using-stl-algorithms written by http://stackoverflow.com/users/14065/loki-astari

ii_t<uint> xi_in(xiFileId);
copy(xi_in, ii_t<uint>(), xi.data()); //xi is a 2D boost::multi_array 

//my efforts to debug

ios::iostate s = xiFileId.rdstate();

if(s & ios::badbit) cout << "bad bit is set" << endl;
if (s & ios::failbit) cout << "fail bit is set" << endl;
if (s & ios::eofbit) cout << "eof bit is set" << endl;
if (s & ios::goodbit) cout << "good bit is set" << endl;

xiFileId.close(); //this line creates the seg violation

发现设置了failbiteof位。使用 valgrind 发现我的整个程序没有内存泄漏。

对另一个二进制文件重复相同的代码(如上),并且在关闭该文件(该文件较早关闭)时不会出现段错误,即使该文件也同时设置了 fail 和 eof 位。

通过使用 gdb 和核心文件识别文件关闭导致段错误,如下所示。

#0  0x00007f16ad99ae50 in __libc_free (mem=0x1b8f930) at malloc.c:3724
3724    malloc.c: No such file or directory.
in malloc.c
(gdb) bt
#0  0x00007f16ad99ae50 in __libc_free (mem=0x1b8f930) at malloc.c:3724
#1  0x00007f16ae1adf0e in std::basic_filebuf<char, std::char_traits<char>   >::_M_destroy_internal_buffer() () from /usr/lib/libstdc++.so.6

#2  0x00007f16ae1af4d4 in std::basic_filebuf<char, std::char_traits<char> >::close() () from /usr/lib/libstdc++.so.6
#3  0x00007f16ae1b133d in std::basic_ifstream<char, std::char_traits<char> >::close() () from /usr/lib/libstdc++.so.6
#4  0x000000000040c119 in main (argc=19, argv=0x7fff05849898) at prediction.cpp:161

如果我删除 xiFileId.close();,因为编译器将在文件超出范围时关闭文件,gdb 回溯会给出以下信息:

#0  0x00007f97fab81e50 in __libc_free (mem=0x15a7930) at malloc.c:3724
3724    malloc.c: No such file or directory.
in malloc.c
(gdb) bt
#0  0x00007f97fab81e50 in __libc_free (mem=0x15a7930) at malloc.c:3724
#1  0x00007f97fb394f0e in std::basic_filebuf<char, std::char_traits<char> >::_M_destroy_internal_buffer() () from /usr/lib/libstdc++.so.6
#2  0x00007f97fb3964d4 in std::basic_filebuf<char, std::char_traits<char> >::close() () from /usr/lib/libstdc++.so.6
#3  0x00007f97fb39c966 in std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream() () from /usr/lib/libstdc++.so.6
#4  0x000000000040c184 in main (argc=19, argv=0x7fff59b71918) at prediction.cpp:163

这表明调用了 ~basic_ifstream() 并且发生了分段违规。

在什么情况下文件关闭会导致段违规?

关于如何进一步调查/修复它的任何想法?

此代码在 Ubuntu 10.04 上运行并使用 gcc 版本 4.4.3 编译。

确定

最佳答案

解决方案在评论中已经提到,为了方便以后的读者,单独贴出来作为答案。

问题出在ii_t类,Segmentation fault on boost::multi_array中提供了解决方案 通过 https://stackoverflow.com/users/12711/michael-burr

关于c++ - 关闭二进制文件时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8003532/

相关文章:

c++ - 理解类型特征的架构

c++ - isspace 和 isalpha 不算数

c++ - 调用recv()会导致段错误

c - 使用 realloc 修改二维数组以添加行和列的函数

XMLHttpRequest 中表单数据的 Javascript 二进制字符串连接

c++ - 在 MPI-IO 上交织来自不同处理器的二进制数据

c++ - 我如何处理这种情况 C++ sizeof 问题

c++ - 在 Qt 中为形状设置适当的颜色

ruby-on-rails - 在 Rails 4.2 和 Ruby 2.2.0 中接收二进制文件时出错

c++ - 奇怪的字符数组行为