From 8bf71f29ba289af74a827b9f5ffbbf42907bace5 Mon Sep 17 00:00:00 2001
From: Zhu Guodong <zhuguodong0001@163.com>
Date: Wed, 9 Aug 2023 19:52:04 +0800
Subject: [PATCH] auto-apply 0039-fix-double-destry-nnrt-mode.patch

---
 .../runtime/delegate/nnrt/nnrt_delegate.cc    | 31 +++++++++----------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/mindspore/lite/src/runtime/delegate/nnrt/nnrt_delegate.cc b/mindspore/lite/src/runtime/delegate/nnrt/nnrt_delegate.cc
index 15b117ca..fa7b55ff 100644
--- a/mindspore/lite/src/runtime/delegate/nnrt/nnrt_delegate.cc
+++ b/mindspore/lite/src/runtime/delegate/nnrt/nnrt_delegate.cc
@@ -107,19 +107,18 @@ Status NNRTDelegate::BuildKirinNPUModel(DelegateModel<schema::Primitive> *model)
   }
 
   size_t extension_size = nnrt_device_info_.extensions_.size();
-  OH_NN_Extension *extensions = (OH_NN_Extension *)malloc(sizeof(OH_NN_Extension) * extension_size);
-  if (extensions == nullptr) {
-    MS_LOG(ERROR) << "allocate extensions failed";
-    return kLiteNullptr;
-  }
-
+  std::vector<OH_NN_Extension> extensions;
   MS_LOG_DEBUG << "set extensions, item number: " << extension_size;
+  const size_t kExtensionNameMax = 128; // This is a length limitation in NNRT API.
   for (size_t i = 0; i < extension_size; i++) {
     auto &src_extension = nnrt_device_info_.extensions_[i];
-    strcpy(extensions[i].name, src_extension.name.c_str());
-    extensions[i].value = (char *)((void *)src_extension.value.data());
-    extensions[i].valueSize = src_extension.value.size();
-    MS_LOG_DEBUG << "set extension, item name: " << extensions[i].name << ", value size: " << extensions[i].valueSize;
+    OH_NN_Extension dst_extension;
+    dst_extension.name[kExtensionNameMax - 1] = '\0';
+    strncpy(dst_extension.name, src_extension.name.c_str(), kExtensionNameMax - 1);
+    dst_extension.value = (char *)((void *)src_extension.value.data());
+    dst_extension.valueSize = src_extension.value.size();
+    extensions.push_back(dst_extension);
+    MS_LOG_DEBUG << "set extension, item name: " << dst_extension.name << ", value size: " << dst_extension.valueSize;
   }
 
   if (IsCustomModel()) {
@@ -131,8 +130,7 @@ Status NNRTDelegate::BuildKirinNPUModel(DelegateModel<schema::Primitive> *model)
     }
   } else {
     SetKirinModelInputsAndOutputs(nn_model);
-
-    auto ret = OH_NNModel_BuildFromMetaGraph(nn_model, meta_graph_, extensions, extension_size);
+    auto ret = OH_NNModel_BuildFromMetaGraph(nn_model, meta_graph_, extensions.data(), extensions.size());
     if (ret != OH_NN_SUCCESS) {
       MS_LOG(ERROR) << "Build NNModel failed, ret: " << ret;
       OH_NNModel_Destroy(&nn_model);
@@ -143,7 +141,7 @@ Status NNRTDelegate::BuildKirinNPUModel(DelegateModel<schema::Primitive> *model)
   auto ret2 =  CreateFullModelKernel(model, nn_model);
   if (ret2 != kSuccess) {
     MS_LOG(ERROR) << "Create full model kernel failed, ret: " << ret2;
-    OH_NNModel_Destroy(&nn_model);
+    return kLiteError;
   }
   return kSuccess;
 }
@@ -185,13 +183,13 @@ Status NNRTDelegate::CreateFullModelKernel(DelegateModel<schema::Primitive> *mod
     OH_NNModel_Destroy(&nn_model);
     return kLiteError;
   }
+  OH_NNModel_Destroy(&nn_model);
   MS_LOG(DEBUG) << "NNRTDelegate creates NNCompilation success.";
 
   auto ret_code = InitNNCompilation(nn_compilation);
   if (ret_code != kSuccess) {
     MS_LOG(ERROR) << "Init NNCompilation failed";
     OH_NNCompilation_Destroy(&nn_compilation);
-    OH_NNModel_Destroy(&nn_model);
     return kLiteError;
   }
 
@@ -200,18 +198,17 @@ Status NNRTDelegate::CreateFullModelKernel(DelegateModel<schema::Primitive> *mod
   if (nn_executor == nullptr) {
     MS_LOG(ERROR) << "Construct NNExecutor failed, ret: " << ret_code;
     OH_NNCompilation_Destroy(&nn_compilation);
-    OH_NNModel_Destroy(&nn_model);
     return kLiteError;
   }
+  OH_NNCompilation_Destroy(&nn_compilation);
 
   auto nnrt_model_kernel = new (std::nothrow)NNRTModelKernel(nn_executor, model->inputs(), model->outputs());
   if (nnrt_model_kernel == nullptr) {
+    OH_NNExecutor_Destroy(&nn_executor);
     MS_LOG(ERROR) << "new NNRTModelKernel failed";
     return kLiteError;
   }
   model->Replace(model->BeginKernelIterator(), model->EndKernelIterator(), nnrt_model_kernel);
-  OH_NNCompilation_Destroy(&nn_compilation);
-  OH_NNModel_Destroy(&nn_model);
   return kSuccess;
 }
 #endif
-- 
2.34.1

