From c494d8fd18c29f601829f428a327b09ac7c29aed Mon Sep 17 00:00:00 2001
From: locasxe <71017860+locasxe@users.noreply.github.com>
Date: 星期三, 25 九月 2024 23:25:18 +0800
Subject: [PATCH] fix onnxruntime memoryleak when load model (#2108)

---
 runtime/onnxruntime/src/fsmn-vad.h   |    4 +++-
 runtime/onnxruntime/src/fsmn-vad.cpp |   11 +++++++----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/runtime/onnxruntime/src/fsmn-vad.cpp b/runtime/onnxruntime/src/fsmn-vad.cpp
index 42ce83b..e347670 100644
--- a/runtime/onnxruntime/src/fsmn-vad.cpp
+++ b/runtime/onnxruntime/src/fsmn-vad.cpp
@@ -60,18 +60,19 @@
         LOG(ERROR) << "Error when load vad onnx model: " << e.what();
         exit(-1);
     }
-    GetInputOutputInfo(vad_session_, &vad_in_names_, &vad_out_names_);
+    GetInputOutputInfo(vad_session_, &vad_in_names_, &vad_out_names_, &vad_allocator);
 }
 
 void FsmnVad::GetInputOutputInfo(
         const std::shared_ptr<Ort::Session> &session,
-        std::vector<const char *> *in_names, std::vector<const char *> *out_names) {
+        std::vector<const char *> *in_names, std::vector<const char *> *out_names,
+        Ort::AllocatorWithDefaultOptions *allocator) {
     Ort::AllocatorWithDefaultOptions allocator;
     // Input info
     int num_nodes = session->GetInputCount();
     in_names->resize(num_nodes);
     for (int i = 0; i < num_nodes; ++i) {
-        std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetInputNameAllocated(i, allocator);
+        std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetInputNameAllocated(i, *allocator);
         Ort::TypeInfo type_info = session->GetInputTypeInfo(i);
         auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
         ONNXTensorElementDataType type = tensor_info.GetElementType();
@@ -90,7 +91,7 @@
     num_nodes = session->GetOutputCount();
     out_names->resize(num_nodes);
     for (int i = 0; i < num_nodes; ++i) {
-        std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetOutputNameAllocated(i, allocator);
+        std::unique_ptr<char, Ort::detail::AllocatedFree> name = session->GetOutputNameAllocated(i, *allocator);
         Ort::TypeInfo type_info = session->GetOutputTypeInfo(i);
         auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
         ONNXTensorElementDataType type = tensor_info.GetElementType();
@@ -310,6 +311,8 @@
 }
 
 FsmnVad::~FsmnVad() {
+    for (auto vad_in_name_item : vad_in_names_) vad_allocator.Free((void*)vad_in_name_item);
+    for (auto vad_out_name_item : vad_out_names_) vad_allocator.Free((void*)vad_out_name_item);
 }
 
 FsmnVad::FsmnVad():env_(ORT_LOGGING_LEVEL_ERROR, ""),session_options_{} {
diff --git a/runtime/onnxruntime/src/fsmn-vad.h b/runtime/onnxruntime/src/fsmn-vad.h
index f06a965..5443a3b 100644
--- a/runtime/onnxruntime/src/fsmn-vad.h
+++ b/runtime/onnxruntime/src/fsmn-vad.h
@@ -34,6 +34,7 @@
     std::shared_ptr<Ort::Session> vad_session_ = nullptr;
     Ort::Env env_;
     Ort::SessionOptions session_options_;
+    Ort::AllocatorWithDefaultOptions vad_allocator;
     std::vector<const char *> vad_in_names_;
     std::vector<const char *> vad_out_names_;
     std::vector<std::vector<float>> in_cache_;
@@ -56,7 +57,8 @@
 
     static void GetInputOutputInfo(
             const std::shared_ptr<Ort::Session> &session,
-            std::vector<const char *> *in_names, std::vector<const char *> *out_names);
+            std::vector<const char *> *in_names, std::vector<const char *> *out_names,
+            Ort::AllocatorWithDefaultOptions *allocator);
 
     void FbankKaldi(float sample_rate, std::vector<std::vector<float>> &vad_feats,
                     std::vector<float> &waves);

--
Gitblit v1.9.1