引起分割错误的法典部分如下。
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
发现<代码>failbit和eof
bit。 使用<代码>valgrind 发现我的整个方案没有传闻。
另一种双亲档案重复使用同一代码(如上所述),而即使该档案同时存在故障,在关闭该档案(该档案早就关闭)时不会出现分辨率过错。
该档案的关闭造成分化错误,其方法是使用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
which shows that the ~basic_ifstream()
is called and segmentation violation occurs.
在什么条件下,档案关闭会造成所谓的侵权行为?
Any ideas on how can I investigate further/fix it?
该代码在Utub10.04上运行,使用4.4.3版汇编。
保证书