【问题标题】:boost::iostreams::mapped_file_sink throws unknown exceptionboost::iostreams::mapped_file_sink 抛出未知异常
【发布时间】:2012-11-14 23:27:18
【问题描述】:

你们能帮我破译boost::iostreams::mapped_file_sink 抛出的未知异常吗?

我的配置

  • 提升 1.51
  • Windows 7 上的 Visual Studio 2012
  • Ubuntu 上的 GCC 4.7

这是我的代码

try
{
    boost::iostreams::mapped_file_params params_;
    boost::iostreams::mapped_file_sink sink_;
    params_.length = 0;
    params_.new_file_size = 1024;
    params_.path = "./test.bin";
    sink_.open(params_);
    sink_.close();
}
catch (std::ios::failure& ex)
{
    std::cout << "\t" << "what: " << ex.what() << "\n";
}
catch (std::system_error& ex)
{
    std::cout << "\t" << "code: " << ex.code() << "  what: " << ex.what() << "\n";
}
catch (std::runtime_error& ex)
{
    std::cout << "\t" << ex.what() << "\n";
}
catch (boost::archive::archive_exception& ex)
{
    std::cout << "\t" << ex.what() << "\n";
}
catch (boost::exception& ex)
{
    std::cout << "blah\n";
}
catch (std::exception& ex)
{
    std::cout << "\t" << ex.what() << " --- " << typeid(ex).name() << "\n";
}

它总是在 Windows 中工作。

在 Ubuntu 中,它会创建给定大小的空文件,但会在 open() 上引发异常。后续执行代码if存在不会导致异常。

最糟糕的是我看不到异常的原因。我只能捕获std::exceptionwhat() 返回无意义的“std::exception”。

为了找出问题所在,我输出了typeid(ex).name(),这表明

N5boost16exception_detail10clone_implINS0_19error_info_injectorISt9exception

根据谷歌的意思是:boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;std::exception&gt; &gt;

有什么想法吗?

【问题讨论】:

    标签: c++ linux exception boost memory-mapped-files


    【解决方案1】:

    您可以在调试器中运行代码并在实际引发异常的函数中设置断点,例如__cxa_throw。您的系统上的函数名称可能不同:使用nm -po program | less 并搜索包含throw 的函数。在那些看起来最有可能是由系统创建的断点中设置一个断点。如果只有很少的异常被抛出,你也可以在std::exception::exception()中设置断点。

    【讨论】:

    • 感谢您的提示!由于我在 Visual Studio 中开发,所以我对 Linux 工具不是很熟悉。
    【解决方案2】:

    经过 50 分钟的猜测,我发现问题出在 length 字段中。文档没有说,但它的默认值必须是 -1,如源代码中所述

    BOOST_STATIC_CONSTANT(size_type, max_length = static_cast<size_type>(-1));
    

    我凭直觉假设如果我将new_file_size 设置为大于零,它将忽略length

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多