English 中文(简体)
关闭双亲档案时的分离错误
原标题:Segmentation fault on closing a binary file

引起分割错误的法典部分如下。

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版汇编。

保证书

问题回答

这些评论已经提到这一解决办法,但为了未来读者的方便,这一解决办法被恰当地列为一种答案。

The problem was with the ii_t class and a solution was provided in Segmentation fault on boost::multi_array by https://stackoverflow.com/users/12711/michael-burr





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签