3

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:

  • GrpcDllTestClient contains a build event that uses protoc.exe to generate the client code from the proto file inside Dependnecies\GreetServer. The command runs of protoc.exe can be found inside the powershell scripts GenerateGrpcClient.ps1. The generated client code (.h and .cc files) are automatically included for build.

  • GrpcDllTestClientHarness is an exe project that references GrpcDllTestClient and uses the generated gRPC client code exposed from GrpcDllTestClient to 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.

Community
  • 1
  • 1
Kakalokia
  • 3,191
  • 3
  • 24
  • 42
  • I did not understand this sentence `if the client is used from outside the DLL` what you mean by `outside` ? – Landstalker Mar 07 '20 at 23:09
  • @Landstalker - The generated C++ stub is built inside the DLL project, and exposed through a factory to any consumer of the DLL project. If you examine the provided example, the ClientFactory would be the place where the stub is exposed. – Kakalokia Mar 07 '20 at 23:25

0 Answers0