Search code examples
c++windowsvisual-c++memory-corruptionmpir

Memory corruption error on Windows build of MPIR


I'm running into a memory corruption error with the Windows version of MPIR, which shows up in the following minimal test case.

Microsoft (R) C/C++ Optimizing Compiler Version 19.25.28610.4 for x64

Compiled MPIR from the github checkout (have verified just now that the problem still reproduces with the very latest version) with this script:

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

Using this source file as a test case:

#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;
}

Compiling like this:

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

And running the resulting program, produces correct output, but on exit shows a heap corruption, with an error message in a pop-up window that unfortunately does not allow copy paste, but it's in

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

line 996, which is

        _ASSERTE(__acrt_first_block == header);

Is this a problem with a known solution? Or is there a known way to track down exactly what's going wrong?


Solution

  • I never did track down the exact cause of the problem, but I think it had something to do with the build process, which used MSBuild via many layers of abstraction, making it almost impossible to be sure exactly what was really going on.

    I've put together a distribution of MPIR using a simple transparent build process, and linking with this version, the problem doesn't occur.

    https://github.com/russellw/mpir