Skip to content

Commit 698b305

Browse files
Replaced raw with smart pointers to solve the memory leak issue. Problem was on ros_segment_inference
1 parent af0c6cc commit 698b305

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

inc/yolo_inference.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class YOLO_V8
8080

8181
private:
8282
Ort::Env env;
83-
Ort::Session* session;
83+
std::unique_ptr<Ort::Session> session;
8484
bool cudaEnable;
8585
Ort::RunOptions options;
8686
std::vector<const char*> inputNodeNames;

src/main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <fstream>
66
#include <random>
77

8-
void Detector(YOLO_V8*& p) {
8+
void Detector(std::unique_ptr<YOLO_V8>& p) {
99
std::filesystem::path current_path = std::filesystem::current_path();
1010
std::filesystem::path imgs_path = current_path / "images";
1111
for (auto& i : std::filesystem::directory_iterator(imgs_path))
@@ -58,7 +58,7 @@ void Detector(YOLO_V8*& p) {
5858
}
5959

6060

61-
void Classifier(YOLO_V8*& p)
61+
void Classifier(std::unique_ptr<YOLO_V8>& p)
6262
{
6363
std::filesystem::path current_path = std::filesystem::current_path();
6464
std::filesystem::path imgs_path = current_path;// / "images"
@@ -97,7 +97,7 @@ void Classifier(YOLO_V8*& p)
9797

9898

9999

100-
int ReadCocoYaml(YOLO_V8*& p) {
100+
int ReadCocoYaml(std::unique_ptr<YOLO_V8>& p) {
101101
// Open the YAML file
102102
std::ifstream file("coco.yaml");
103103
if (!file.is_open())
@@ -148,7 +148,9 @@ int ReadCocoYaml(YOLO_V8*& p) {
148148

149149
void DetectTest()
150150
{
151-
YOLO_V8* yoloDetector = new YOLO_V8;
151+
//YOLO_V8* yoloDetector = new YOLO_V8;
152+
std::unique_ptr<YOLO_V8> yoloDetector = std::make_unique<YOLO_V8>();
153+
152154
ReadCocoYaml(yoloDetector);
153155
DL_INIT_PARAM params;
154156
params.rectConfidenceThreshold = 0.1;
@@ -177,7 +179,7 @@ void DetectTest()
177179

178180
void ClsTest()
179181
{
180-
YOLO_V8* yoloDetector = new YOLO_V8;
182+
std::unique_ptr<YOLO_V8> yoloDetector = std::make_unique<YOLO_V8>();
181183
std::string model_path = "cls.onnx";
182184
ReadCocoYaml(yoloDetector);
183185
DL_INIT_PARAM params{ model_path, YOLO_CLS, {224, 224} };

src/yolo_inference.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ YOLO_V8::YOLO_V8() {
99

1010

1111
YOLO_V8::~YOLO_V8() {
12-
delete session;
12+
for (auto& name : inputNodeNames) {
13+
delete[] name;
14+
}
15+
for (auto& name : outputNodeNames) {
16+
delete[] name;
17+
}
1318
}
1419

1520
#ifdef USE_CUDA
@@ -95,6 +100,19 @@ char* YOLO_V8::PreProcess(const cv::Mat& iImg, std::vector<int> iImgSize, cv::Ma
95100

96101
const char* YOLO_V8::CreateSession(DL_INIT_PARAM& iParams) {
97102
const char* Ret = RET_OK;
103+
if (session) {
104+
105+
// Clear node names
106+
for (auto& name : inputNodeNames) {
107+
delete[] name;
108+
}
109+
inputNodeNames.clear();
110+
111+
for (auto& name : outputNodeNames) {
112+
delete[] name;
113+
}
114+
outputNodeNames.clear();
115+
}
98116
std::regex pattern("[\u4e00-\u9fa5]");
99117
bool result = std::regex_search(iParams.modelPath, pattern);
100118
if (result)
@@ -132,7 +150,7 @@ const char* YOLO_V8::CreateSession(DL_INIT_PARAM& iParams) {
132150
const char* modelPath = iParams.modelPath.c_str();
133151
#endif // _WIN32
134152

135-
session = new Ort::Session(env, modelPath, sessionOption);
153+
session = std::make_unique<Ort::Session>(env, modelPath, sessionOption);
136154
Ort::AllocatorWithDefaultOptions allocator;
137155
size_t inputNodesNum = session->GetInputCount();
138156
for (size_t i = 0; i < inputNodesNum; i++)

0 commit comments

Comments
 (0)