Boost mapped_file_sink抛出异常

boost mapped_file_sink throw exception

本文关键字:抛出异常 sink file Boost mapped      更新时间:2023-10-16

我不知道为什么当我创建mapped_file_sink对象时抛出异常。

异常:

terminate called after throwing an instance of 
'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::exception> >'
int main(int argc,char *argv[])
{
        boost::iostreams::mapped_file_sink sink(argv[1]);
        char temp[] ="AAAA";
        memcpy(sink.data(), temp, sizeof(temp));
        sink.close();
        return 1;
}

很可能文件AAAA不存在。为了创建文件,您需要使用boost::iostreams::mapped_file_params变量打开内存映射文件,其new_file_size字段将设置为所需的文件大小。

在64位Linux虚拟机上使用boost 1.53,我也遇到了上面报告的异常。

我最终发现是权限问题。当我从虚拟盒共享文件夹运行我的测试时,我可以创建和大小文件,但是当BOOST_IOSTREAMS_FD_MMAP调用cleanup_and_throw("失败的映射文件")失败时,我会得到神秘的异常。

在抛出enable_current_exception(enable_error_info(e));调用"failed mapping file: invalid argument"消息丢失。

如果从我的主文件夹运行相同的代码,api将按预期工作。

所以我的问题的本质归结为权限。我希望这能帮助到其他遇到这种情况的人。

drwxrwx——root vboxsf

drwxr-xr-x user user