How to delete Speech::Stub and grpc:Channel

70 views
Skip to first unread message

cecili...@genesys.com

unread,
May 6, 2020, 11:52:18 AM5/6/20
to grpc.io
Here is a snippet of our C++ code...

std::shared_ptr<grpc::ChannelCredentials> m_pChanCreds;
std::shared_ptr<grpc::ChannelCredentials> m_pCreds;
std::shared_ptr<grpc::CallCredentials> m_pCallCreds;
std::unique_ptr<Speech::Stub> m_pSpeechStub;
m_pChanCreds = grpc::SslCredentials(grpc::SslCredentialsOptions());
m_pCallCreds = grpc::ServiceAccountJWTAccessCredentials(key, 3600);
m_pCreds = grpc::CompositeChannelCredentials(m_pChanCreds, m_pCallCreds);

auto channel = grpc::CreateChannel(m_strResourceURI, m_pCreds);
m_pSpeechStub = Speech::NewStub(channel);
To clean up m_pCallCreds, m_pCreds, channel and m_pSpeechStub, is it sufficient to just set m_pSpeechStub to NULL? 

How long does it take for the channel to be terminated after the stub is set to NULL? Is there anything else we could/should do to terminate and tear down the channel? 

What about the credentials? Anything we need to do to destroy them?

Thanks.

Mark D. Roth

unread,
May 6, 2020, 1:52:44 PM5/6/20
to grpc.io
The stub holds a ref to the channel, but every pending call also takes its own ref to the channel.  So the channel won't be destroyed until you destroy m_pSpeechStub, reset or destroy channel, and complete any calls that you started using the stub.

The channel takes a ref to the channel creds and call creds.  You need to release the refs you're holding in the shared_ptr variables, but otherwise they should be destroyed when the channel is destroyed.

cecili...@genesys.com

unread,
May 7, 2020, 9:07:59 AM5/7/20
to grpc.io
Many thanks! That is very helpful. Just to confirm...

To destroy m_pSpeechStub, set m_pSpeechStub to NULL.

Assume we save the channel like this
std::shared_ptr<grpc::Channel> m_pChannel;
m_pChannel = grpc::CreateChannel(m_strResourceURI, m_pCreds);

To destroy m_pChannel, set m_pChannel to NULL after setting m_pSpeechStub to NULL.

To destroy m_pCreds, set m_pCreds to NULL after setting m_pChannel to NULL.

To destroy m_pCallCreds, set m_pCallCreds to NULL after setting m_pCreds to NULL.

Reply all
Reply to author
Forward
0 new messages