0

我很难理解为什么我的一些使用 boost 的代码在 Visual Studio 2017 下运行良好,现在却在 Visual Studio 2019 下导致访问冲突。但是,我只在 Debug 构建下遇到此故障。发布构建工作正常,没有问题。

我在构建、环境或代码中设置不正确会导致这样的失败吗?

我的环境:

  • 视窗 10
  • Boost 1.74(动态链接)
  • Visual Studio 2019 v16.7.6
  • 为 C++ x64 编译

我的代码的失败行是这样的:

boost::filesystem::path dir = (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("%%%%-%%%%-%%%%-%%%%"));

Boost 文件系统中的失败行是这里boost/filesystem/path.hpp

namespace path_traits
{  //  without codecvt

  inline
    void convert(const char* from,
    const char* from_end,    // 0 for null terminated MBCS
    std::wstring & to)
  {
    convert(from, from_end, to, path::codecvt());
  }

Visual Studio 上报的失败信息如下:

Exception thrown at 0x00007FF9164F1399 (vcruntime140d.dll) in ezv8.tests.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

调用堆栈如下所示:

    vcruntime140d.dll!00007ff9164f1550()    Unknown
>   boost_filesystem-vc142-mt-gd-x64-1_74.dll!wmemmove(wchar_t * _S1, const wchar_t * _S2, unsigned __int64 _N) Line 248    C++
    boost_filesystem-vc142-mt-gd-x64-1_74.dll!std::_WChar_traits<wchar_t>::move(wchar_t * const _First1, const wchar_t * const _First2, const unsigned __int64 _Count) Line 204 C++
    boost_filesystem-vc142-mt-gd-x64-1_74.dll!std::wstring::append(const wchar_t * const _Ptr, const unsigned __int64 _Count) Line 2864 C++
    boost_filesystem-vc142-mt-gd-x64-1_74.dll!std::wstring::append<wchar_t *,0>(wchar_t * const _First, wchar_t * const _Last) Line 2916    C++
    boost_filesystem-vc142-mt-gd-x64-1_74.dll!`anonymous namespace'::convert_aux(const char * from, const char * from_end, wchar_t * to, wchar_t * to_end, std::wstring & target, const std::codecvt<wchar_t,char,_Mbstatet> & cvt) Line 77 C++
    boost_filesystem-vc142-mt-gd-x64-1_74.dll!boost::filesystem::path_traits::convert(const char * from, const char * from_end, std::wstring & to, const std::codecvt<wchar_t,char,_Mbstatet> & cvt) Line 153   C++
    appsvcs.dll!boost::filesystem::path_traits::convert(const char * from, const char * from_end, std::wstring & to) Line 1006  C++
    appsvcs.dll!boost::filesystem::path_traits::dispatch<std::wstring>(const std::string & c, std::wstring & to) Line 257   C++
    appsvcs.dll!boost::filesystem::path::path<char [20]>(const char[20] & source, void * __formal) Line 168 C++

我在整个代码中使用 UTF-8 字符串,因此我将 boost::filesystem 配置为期望 UTF-8 字符串,如下所示:

boost::nowide::nowide_filesystem();

4

1 回答 1

0

这个问题的原因原来是使用不一致_ITERATOR_DEBUG_LEVEL。此设置确实会影响 ABI 兼容性。我在自己的代码中设置了这个标志(为 0),但它没有在 Boost 构建中设置。解决方案是从自己的代码中删除标志,或者通过添加define=_ITERATOR_DEBUG_LEVEL=0到 b2 参数(来自另一个堆栈溢出答案)将标志添加到 Boost 构建中。

于 2020-11-20T05:57:08.213 回答