0

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?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 1
    Do you build a release version of `mpir.lib` with this - `call msbuild.bat gc LIB x64 Release`? And then link this `mpir.lib` to your exe using multithreaded debug version of the CRT (`/MTd`)? – nevilad Mar 18 '21 at 17:21
  • @nevilad Well, currently using the debug build of `mpir.lib`, but otherwise with those flags, yes. – rwallace Mar 19 '21 at 15:44
  • 1
    So there is no mix of debug/release CRT versions. Is `mpir` built with `/MTd` too? Mixing multithreaded and single threaded CRT is another common source of such errors. – nevilad Mar 19 '21 at 16:45
  • @nevilad As far as I could tell, yes. But you are probably right that the problem was somewhere in the build process; I've posted an answer that expands further on that. – rwallace Mar 19 '21 at 17:54

1 Answers1

0

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

rwallace
  • 31,405
  • 40
  • 123
  • 242