I am trying to generate a gRPC client in C++, and have the generated client code built inside a Visual Studio 2015 DLL project. For some reason when building the generated gRPC client in a DLL a crash occrus when making a gRPC request, however building the generated gRPC client in a static lib does not.
I have a reproducible example that can be found here. The example contains the server to run inside the Dependencies\GreetServer folder, and two C++ projects:
GrpcDllTestClientcontains a build event that usesprotoc.exeto generate the client code from the proto file insideDependnecies\GreetServer. The command runs ofprotoc.execan be found inside the powershell scriptsGenerateGrpcClient.ps1. The generated client code (.h and .cc files) are automatically included for build.GrpcDllTestClientHarnessis an exe project that referencesGrpcDllTestClientand uses the generated gRPC client code exposed fromGrpcDllTestClientto fire requests.
The example contains two Visual Studio build configurations. Debug builds the GrpcDllTestClient as a DLL, which is the configuration where the crash happens, DebugAsLib builds the client project as static lib, which doesn't cause the crash.
When the crash occurs, following is the call stack that I can see:
> GrpcDllTestClientHarness.exe!gpr_atm_no_barrier_fetch_add(int * p, int delta) Line 101 C++
GrpcDllTestClientHarness.exe!grpc_core::CallCombiner::SetNotifyOnCancel(grpc_closure * closure) Line 189 C++
GrpcDllTestClientHarness.exe!grpc_call_unref(grpc_call * c) Line 604 C++
GrpcDllTestClientHarness.exe!grpc_impl::ClientContext::~ClientContext() Line 70 C++
I have installed gRPC using vcpkg with the following options:
set(VCPKG_TARGET_ARCHITECTURE x86)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)
set(VCPKG_PLATFORM_TOOLSET v140)
The options make vcpkg build gRPC libraries as static libs, targeting the Visual Studio 2015 compiler.
Any idea why building the generated gRPC client inside a DLL causes a crash if the client is used from outside the DLL? I'm happy to go with the static lib options, but interested to know if someone came across the issue before, and if there is a workaround.