boost::iostreams::mapped_file_source::open 在 Windows 上导致退出代码 3 但在 Ubuntu 上有效

boost::iostreams::mapped_file_source::open causes exit code 3 on Windows but works in Ubuntu

我正在使用 boost::iostreams namespace 中的 mapped_file_source 来分块读取大文件:

boost::iostreams::mapped_file_source read_bytes(const char *file_path,
                                                unsigned long long int offset,
                                                unsigned long long int length) {
    iostreams::mapped_file_params parameters;
    parameters.path = file_path;
    parameters.length = static_cast<size_t>(length);
    parameters.flags = iostreams::mapped_file::mapmode::readonly;
    parameters.offset = static_cast<boost::iostreams::stream_offset>(offset);

    boost::iostreams::mapped_file_source file;

    file.open(parameters);

    if (file.is_open()) {
        return file;
    } else {
        printf("Failed to open file\n");
        exit(EXIT_FAILURE);
    }
}

我的代码在 WSL (Windows Subsystem for Linux) 中对 Ubuntu 工作正常,但是当我在 Windows 上编译和 运行 时,第二个 file.open 调用导致进程退出,退出代码为 3

Reading file in 5 parts
Processing chunk 1/5
Processing chunk 2/5

Process finished with exit code 3

没有出现错误信息或异常。 documentation 表明它是 ERROR_PATH_NOT_FOUND 但这没有任何意义。

我调试了两个平台二进制文件,所有变量都完全相同,唯一的例外是 Unix 样式文件路径和 Windows 样式路径以及分配的地址和系统时间变量,所以没有发生内存损坏。我不明白为什么这在 Windows 上不起作用,而它的行为应该相同。

我正在使用 MinGWWindowsgcc 8.2 编译 Ubuntu

"C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\x86_64-w64-mingw32-gcc.exe" --version
x86_64-w64-mingw32-gcc.exe (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

如果我一次读取文件,它工作正常 (!)。我将所有偏移量与页面大小的倍数对齐。 mapped_file_source 在超出范围时会自动关闭,因此这不是 "file already open" 问题(这实际上会导致 Boost 出现异常)。

使用MSVC the problem can no longer be reproduced now. In general, using Microsoft's compiler for Windows may be more reliable than the likes of MinGW, especially since I was using an "unofficial" toolchain.