I am porting a project from MSVC to mingw. The project contains C and C++. However, whenever an exception is thrown, instead of being caught, std::terminate gets called and the application crashes. I can't understand why so any advice would be appreciated.
My toolchain is cmake/ninja/mingw32 in a MSYS2 environment installed in Windows.
MCVE:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(FailedExceptions)
add_executable(FailedExceptions c_funcs.c main.cpp)
//main.cpp
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
int main() {
try {
boost::property_tree::ptree pt;
std::printf("reading file\n");
boost::property_tree::read_xml("nonexistant-file", pt);
std::printf("provider file read\n");
} catch (...) {
std::printf("Exception caught\n");
}
return 0;
}
// c_funcs.c
int SomeCFunction()
{
return 0;
}
Output
$ cmake .. -GNinja
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: C:/msys64/mingw32/bin/cc.exe -- works
-- Check for working CXX compiler: C:/msys64/mingw32/bin/c++.exe -- works
-- Configuring done
-- Generating done
-- Build files have been written to: C:/msys64/home/sferguson/src/vis/build
$ ninja -v
[1/3] C:\msys64\mingw32\bin\cc.exe -MD -MT c_funcs.c.obj -MF c_funcs.c.obj.d -o c_funcs.c.obj -c ../c_funcs.c
[2/3] C:\msys64\mingw32\bin\c++.exe -MD -MT main.cpp.obj -MF main.cpp.obj.d -o main.cpp.obj -c ../main.cpp
[3/3] C:\msys64\mingw32\bin\c++.exe c_funcs.c.obj main.cpp.obj -o FailedExceptions.exe -Wl,--major-image-version,0,--minor-image-version,0 -lgcc_eh -lgcc_eh -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
$ ./FailedExceptions.exe
reading file
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
$
Trace:
I can get this trace from Dr. Mingw. It really does appear that the crash happens between construction of the exception and the actual throw.
ntdll.dll!_NtTerminateProcess@8
ntdll.dll!_RtlExitUserProcess@4
kernel32.dll!_ExitProcessStub@4
msvcrt.dll!___crtExitProcess
msvcrt.dll!__cinit
msvcrt.dll!__exit
msvcrt.dll!_abort
FailedExceptions.exe!uw_init_context_1
FailedExceptions.exe!boost::property_tree::xml_parser::xml_parser_error::xml_parser_error
FailedExceptions.exe!boost::property_tree::xml_parser::read_xml<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >
FailedExceptions.exe!main
FailedExceptions.exe!__tmainCRTStartup [D:/develop/scripts/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c @ 334]
kernel32.dll!@BaseThreadInitThunk@12
ntdll.dll!___RtlUserThreadStart@8
ntdll.dll!__RtlUserThreadStart@8
Troubleshooting:
- I found some posts from 5-10 years ago suggesting that this could be a conflict between mingw's dw2 and sjlj libraries, but I only have the
libgcc_s_dw2-1.dll
binaries installed which come with the mingw-w64-i686-gcc-libs package in the msys pacman repository. - I tried changing my CMakeLists.txt file to compile everything with C++ with
project(FailedExceptions LANGUAGES CXX)
. This prevents cmake from building my C files. So it does work for the MCVE, but my full project is missing all C content. - I've added
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -fexceptions)
but it seems to have no effect. I've confirmed withninja -v
that this flag is added to the C-file compilation command. - As soon as I remove the C file from the build, everything works. But even though I'm not using the C-file in this MCVE, I still use it in my big project.
- I've found another smaller example here. I can reproduce that problem IFF I also compile a C file in that same project.