GRPC 客户端在某些情况下会出现资源无法释放?
当我们创建一个全局的对象时,或者静态对象是,或者智能指针,并期望系统在进程退出的时候自动回收申请的内存。但不幸的是,系统回收资源无法按我们期望的顺序,这就会造成GRPC依赖的某些资源已经释放,而我们自己申请的GRPC的资源有在等待这些资源,造成进程无法结束或者卡死的问题。
如下:
// Normal case
#include <iostream>
struct A {
std::shared_ptr<grpc::Channel> mChan;
std::unique_ptr <TMService::Stub> mGRPCServerStub;
}
int main() {
A a;
a.mChan = grpc::CreateChannel("localhost:88888", grpc::InsecureChannelCredentials());
a.mGRPCServerStub = AService::NewStub(mChan);
grpc::ClientContext context;
ARequest request;
AResponse response;
grpc::Status rpcStatus = a.mGRPCStub->callMethod(&context, request, &response);
if (!rpcStatus.ok()) {
std::cerr << "GRPC FAILD TO CALL METHOD" << std::endl;
} else {
return 0;
}
}
// Abnormal case, 资源无法释放
#include <iostream>
struct A {
std::shared_ptr<grpc::Channel> mChan;
std::unique_ptr <TM