From 17bf7392f588ce0249cc29c03ebeea3c5a1303e3 Mon Sep 17 00:00:00 2001 From: j30052480 Date: Sat, 1 Jun 2024 16:56:01 +0800 Subject: [PATCH] fix: memleak in hdi proxy Signed-off-by: j30052480 --- .../codegen/cpp_client_proxy_code_emitter.cpp | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/framework/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp b/framework/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp index 7952791f5..b610d7caa 100644 --- a/framework/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp +++ b/framework/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp @@ -163,10 +163,20 @@ void CppClientProxyCodeEmitter::EmitProxyConstructor(StringBuilder &sb, const st " : IProxyBroker<%s>(remote) {\n", EmitDefinitionByInterface(interface_, interfaceName_).c_str()); if (!interface_->IsSerializable() && (!interface_->IsCallback())) { sb.Append(prefix + TAB).Append("reconnectRemote_ = nullptr;\n"); + sb.Append(prefix + TAB).Append("servMgr_ = nullptr;\n"); + sb.Append(prefix + TAB).Append("deathRecipient_ = nullptr;\n"); sb.Append(prefix + TAB).Append("isReconnected_ = false;\n"); } sb.Append(prefix).AppendFormat("}\n"); - sb.Append(prefix).AppendFormat("virtual ~%s() = default;\n", proxyName_.c_str()); + if (!interface_->IsSerializable() && (!interface_->IsCallback())) { + sb.Append(prefix).AppendFormat("virtual ~%s() {\n", proxyName_.c_str()); + sb.Append(prefix + TAB).AppendFormat("if (servMgr_ != nullptr && deathRecipient_ != nullptr) {\n"); + sb.Append(prefix + TAB + TAB).AppendFormat("servMgr_->RemoveDeathRecipient(deathRecipient_);\n"); + sb.Append(prefix + TAB).AppendFormat("}\n"); + sb.Append(prefix).AppendFormat("}\n"); + } else { + sb.Append(prefix).AppendFormat("virtual ~%s() = default;\n", proxyName_.c_str()); + } } void CppClientProxyCodeEmitter::EmitProxyMethodDecls(StringBuilder &sb, const std::string &prefix) const @@ -263,6 +273,9 @@ void CppClientProxyCodeEmitter::EmitProxyPublicMembers(StringBuilder &sb, const sb.Append(prefix).Append("bool isReconnected_;\n"); sb.Append(prefix).Append("std::string serviceName_;\n"); sb.Append(prefix).AppendFormat("sptr servMgr_;\n", devmgrVersionName_.c_str()); + sb.Append(prefix).AppendFormat("sptr<%s::%s> deathRecipient_;\n", + EmitDefinitionByInterface(interface_, proxyName_).c_str(), + devmgrDeathRecipientName_.c_str()); sb.Append(prefix).Append("sptr reconnectRemote_;\n"); } @@ -490,10 +503,11 @@ void CppClientProxyCodeEmitter::EmitGetInstanceMethodInitProxyImpl(StringBuilder std::string serMinorName = "serMinorVer"; sb.Append(prefix + TAB).AppendFormat("%s->servMgr_ = ", objName.c_str()); sb.Append("OHOS::HDI::hdi_objcast(servMgr);\n"); - sb.Append(prefix + TAB).AppendFormat("%s->servMgr_->AddDeathRecipient(\n", objName.c_str()); - sb.Append(prefix + TAB + TAB).AppendFormat("new %s::%s(%s));\n", + sb.Append(prefix + TAB).AppendFormat("%s->deathRecipient_ = new %s::%s(%s);\n", objName.c_str(), EmitDefinitionByInterface(interface_, proxyName_).c_str(), devmgrDeathRecipientName_.c_str(), objName.c_str()); + sb.Append(prefix + TAB).AppendFormat("%s->servMgr_->AddDeathRecipient(%s->deathRecipient_);\n", + objName.c_str(), objName.c_str()); sb.Append(prefix + TAB).AppendFormat("%s->isReconnected_ = false;\n", objName.c_str()); sb.Append(prefix + TAB).AppendFormat("%s->serviceName_ = serviceName;\n", objName.c_str()); -- Gitee