Windows 构建 MPIR 时出现内存损坏错误

Memory corruption error on Windows build of MPIR

我 运行 遇到了 Windows 版 MPIR 的内存损坏错误,出现在以下最小测试用例中。

Microsoft (R) C/C++ 针对 x64 优化编译器版本 19.25.28610.4

使用此脚本从 github 结帐编译的 MPIR(刚刚验证问题仍然在最新版本中重现):

pushd \mpir\msvc\vs19
call msbuild.bat gc DLL x64 Debug
call msbuild.bat gc LIB x64 Release
popd
copy \mpir\dll\x64\Debug\mpir.dll

使用此源文件作为测试用例:

#include <stdio.h>
#include <gmp.h>

int main(int argc, const char **argv) {
  mpz_t x;
  mpz_init_set_str(x,"123",10);
  mpz_out_str(stdout,10,x);
  putchar('\n');
  return 0;
}

这样编译:

cl /I\mpir /MTd a.cc \mpir\dll\x64\Debug\mpir.lib

并且 运行 结果程序产生了正确的输出,但在退出时显示堆损坏,并在弹出窗口中显示一条错误消息 window 不幸的是不允许复制粘贴,但是它在

C:\Program Files (x86)\Windows Kits\Source.0.18362.0\ucrt\heap\debug_heap.cpp

第996行,即

        _ASSERTE(__acrt_first_block == header);

这是已知解决方案的问题吗?或者是否有一种已知的方法可以准确地找出问题所在?

我从来没有找到问题的确切原因,但我认为它与构建过程有关,该过程通过许多抽象层使用 MSBuild,几乎不可能确定到底是什么进行中。

我使用简单透明的构建过程整理了一个 MPIR 发行版,并链接到这个版本,没有出现问题。

https://github.com/russellw/mpir