1

I'm on Windows/mingw64 and trying to generate webassembly with the Binaryen C API from a C application. I compiled the binaryen shared library libbinaryen.dll without problems, and tried to reproduce the hello world example. It compiles and executes as expected, but it hangs on exit from main(), both with a return and with exit().

I thought there might be something wrong with exception handling since the Binaryen DLL is C++, but I see the same hang on main() exit whether I compile and link with gcc or g++. Interestingly, if I compile the Binaryen as a static link library (libbinaryen.a) and link that to my executable, the problem goes away, but this requires compiling with g++. And it generates a 16Mb executable, so I'd rather not do that.

I'm using Mingw version x86_64-posix-seh-rev0, gcc 8.1.0, and linking with -lmingw32 -lbinaryen. I have tried linking either directly to the DLL and to the link library, same result.

I'm guessing there is some problem with C/C++ interoperability here, but can't figure out what. The problem also causes gdb to hang upon main() exit, so I haven't been able to pinpoint exactly what happens.

Anyone seen this kind of behavior when calling C++ DLLs from C on mingw?

Roland
  • 121
  • 4

1 Answers1

1

I think I found the cause now, posting it here for reference. It looks like the Binaryen library contains a static object ThreadPool for keeping track of threads, and since it's a DLL this object's destructors are not called when closing down, which leads to a threading deadlock when exiting main(). Limiting Binaryen to a single thread by defining BINARYEN_CORES = 1 prevents instantiating the ThreadPool object and eliminates the problem.

It looks like this is an open issue with mingw/msys, hopefully it will be resolved in the near future.

Roland
  • 121
  • 4