From 86f52e312a43ea12bf9a728e3742dad1d2666d22 Mon Sep 17 00:00:00 2001 From: Matthew Stevenson Date: Mon, 26 Aug 2019 15:37:10 -0700 Subject: [PATCH] More work with memory leaks --- include/grpcpp/security/tls_credentials_options.h | 4 ++++ src/cpp/common/tls_credentials_options.cc | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/grpcpp/security/tls_credentials_options.h b/include/grpcpp/security/tls_credentials_options.h index a3bc60d6125..87c97dbd217 100644 --- a/include/grpcpp/security/tls_credentials_options.h +++ b/include/grpcpp/security/tls_credentials_options.h @@ -84,6 +84,10 @@ class TlsCredentialReloadArg { private: grpc_tls_credential_reload_arg c_arg_; + /** These boolean variables record whether the corresponding field of the C + * arg was dynamically allocated. This occurs e.g. if one of the above setter functions was + * used, or if the C arg's callback function does so. **/ + bool key_materials_config_alloc_ = false; bool error_details_alloc_ = false; }; diff --git a/src/cpp/common/tls_credentials_options.cc b/src/cpp/common/tls_credentials_options.cc index 830c1a4c8dd..5a446cd0354 100644 --- a/src/cpp/common/tls_credentials_options.cc +++ b/src/cpp/common/tls_credentials_options.cc @@ -53,7 +53,10 @@ TlsCredentialReloadArg::TlsCredentialReloadArg( c_arg_ = arg; } -TlsCredentialReloadArg::~TlsCredentialReloadArg() { } +TlsCredentialReloadArg::~TlsCredentialReloadArg() { + if (key_materials_config_alloc_) { gpr_free(c_arg_.key_materials_config); } + if (error_details_alloc_) { gpr_free(const_cast(c_arg_.error_details)); } +} void* TlsCredentialReloadArg::cb_user_data() const { return c_arg_.cb_user_data; @@ -83,8 +86,12 @@ void TlsCredentialReloadArg::set_cb_user_data(void* cb_user_data) { void TlsCredentialReloadArg::set_key_materials_config( const std::shared_ptr& key_materials_config) { + if (key_materials_config_alloc_) { + gpr_free(c_arg_.key_materials_config); + } c_arg_.key_materials_config = ConvertToCKeyMaterialsConfig(key_materials_config); + key_materials_config_alloc_ = true; } void TlsCredentialReloadArg::set_status( @@ -99,7 +106,6 @@ void TlsCredentialReloadArg::set_error_details( } c_arg_.error_details = gpr_strdup(error_details.c_str()); error_details_alloc_ = true; - c_arg_.error_details = gpr_strdup(error_details.c_str()); } void TlsCredentialReloadArg::OnCredentialReloadDoneCallback() {