Skip to content

Commit 0ae0dc9

Browse files
committed
[BUG FIX] fix memory leak for ort backend
1 parent cc8d1f3 commit 0ae0dc9

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

fastdeploy/runtime/backends/ort/ort_backend.cc

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ namespace fastdeploy {
3131
std::vector<OrtCustomOp*> OrtBackend::custom_operators_ =
3232
std::vector<OrtCustomOp*>();
3333

34-
std::wstring ToWstring(const std::string &str) {
34+
std::wstring ToWstring(const std::string& str) {
3535
unsigned len = str.size() * 2;
3636
setlocale(LC_CTYPE, "");
37-
wchar_t *p = new wchar_t[len];
37+
wchar_t* p = new wchar_t[len];
3838
mbstowcs(p, str.c_str(), len);
3939
std::wstring wstr(p);
4040
delete[] p;
@@ -57,12 +57,13 @@ bool OrtBackend::BuildOption(const OrtBackendOption& option) {
5757
session_options_.SetExecutionMode(ExecutionMode(option.execution_mode));
5858
}
5959
if (!option.optimized_model_filepath.empty()) {
60-
#if (defined(_WIN32) || defined(_WIN64))
60+
#if (defined(_WIN32) || defined(_WIN64))
6161
session_options_.SetOptimizedModelFilePath(
62-
ToWstring(option.optimized_model_filepath).c_str());
62+
ToWstring(option.optimized_model_filepath).c_str());
6363
#else
64-
session_options_.SetOptimizedModelFilePath(option.optimized_model_filepath.c_str());
65-
#endif
64+
session_options_.SetOptimizedModelFilePath(
65+
option.optimized_model_filepath.c_str());
66+
#endif
6667
}
6768

6869
#ifdef WITH_DIRECTML
@@ -207,12 +208,17 @@ bool OrtBackend::InitFromPaddle(const std::string& model_buffer,
207208
std::strcpy(charStr, one_type.c_str());
208209
disable_fp16_ops.push_back(charStr);
209210
}
210-
if (!paddle2onnx::Export(
211-
model_buffer.c_str(), model_buffer.size(), params_buffer.c_str(),
212-
params_buffer.size(), &model_content_ptr, &model_content_size, 11,
213-
true, verbose, true, true, true, ops.data(), 2, "onnxruntime",
214-
nullptr, 0, "", &save_external, option.enable_fp16,
215-
disable_fp16_ops.data(), option.ort_disabled_ops_.size())) {
211+
bool is_exported = paddle2onnx::Export(
212+
model_buffer.c_str(), model_buffer.size(), params_buffer.c_str(),
213+
params_buffer.size(), &model_content_ptr, &model_content_size, 11, true,
214+
verbose, true, true, true, ops.data(), 2, "onnxruntime", nullptr, 0, "",
215+
&save_external, option.enable_fp16, disable_fp16_ops.data(),
216+
option.ort_disabled_ops_.size());
217+
for (auto& disable_fp16_op : disable_fp16_ops) {
218+
delete[] disable_fp16_op;
219+
}
220+
disable_fp16_ops.clear();
221+
if (!is_exported) {
216222
FDERROR << "Error occured while export PaddlePaddle to ONNX format."
217223
<< std::endl;
218224
return false;
@@ -258,6 +264,8 @@ bool OrtBackend::InitFromOnnx(const std::string& model_file,
258264
&model_content_ptr, &model_content_size);
259265
std::string onnx_model_proto(model_content_ptr,
260266
model_content_ptr + model_content_size);
267+
delete[] model_content_ptr;
268+
model_content_ptr = nullptr;
261269
onnx_model_buffer = onnx_model_proto;
262270
} else {
263271
onnx_model_buffer = model_file;

0 commit comments

Comments
 (0)