Skip to content

Commit e068ed3

Browse files
Added proper exception handling to avoid segfault if the model path is wrong (#4)
2 parents 78cebcf + 454989f commit e068ed3

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ add_compile_options(-Wextra -Werror=extra)
99
set(CMAKE_CXX_STANDARD 17)
1010
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1111

12-
find_package(console_bridge REQUIRED)
13-
find_package(OpenCV REQUIRED)
1412
find_package(catkin REQUIRED
1513
COMPONENTS
1614
onnxruntime_ros
1715
)
1816

17+
find_package(console_bridge REQUIRED)
18+
find_package(OpenCV REQUIRED)
1919

2020
set(${PROJECT_NAME}_CUDA_ENABLED ${onnxruntime_ros_CUDA_ENABLED})
2121
if(${PROJECT_NAME}_CUDA_ENABLED)
@@ -47,9 +47,9 @@ include_directories(
4747
${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}
4848
SYSTEM
4949
${${PROJECT_NAME}_CUDA_INCLUDE_DIRS}
50+
${console_bridge_INCLUDE_DIRS}
5051
${OpenCV_INCLUDE_DIRS}
5152
${catkin_INCLUDE_DIRS}
52-
${console_bridge_INCLUDE_DIRS}
5353
)
5454

5555
add_library(${PROJECT_NAME}
@@ -59,9 +59,9 @@ add_library(${PROJECT_NAME}
5959
)
6060

6161
target_link_libraries(${PROJECT_NAME}
62+
${console_bridge_LIBRARIES}
6263
${OpenCV_LIBRARIES}
6364
${catkin_LIBRARIES}
64-
${console_bridge_LIBRARIES}
6565
${${PROJECT_NAME}_CUDA_TARGET_LINK_LIBRARIES}
6666
)
6767

src/sam_inference.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,9 @@ const char* SAM::CreateSession(SEG::DL_INIT_PARAM& iParams)
118118
}
119119
catch (const std::exception& e)
120120
{
121-
const char* str1 = "[SAM]: ";
122-
const char* str2 = e.what();
123-
std::string str_result = std::string(str1) + std::string(str2);
124-
char* merged = new char[str_result.length() + 1];
125-
std::strcpy(merged, str_result.c_str());
126-
CONSOLE_BRIDGE_logWarn("%s", merged);
127-
delete[] merged;
128-
return "[SAM]: CreateSession failed.";
121+
std::string error_msg = "[SAM]: Failed to create session: " + std::string(e.what());
122+
CONSOLE_BRIDGE_logWarn("%s", error_msg.c_str());
123+
throw std::runtime_error(error_msg);
129124
}
130125
}
131126

src/utils.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "sam_onnx_ros/utils.hpp"
22

3+
#include <console_bridge/console.h>
4+
35
// #define LOGGING
46

57
// Constructor
@@ -129,7 +131,7 @@ void Utils::PostProcess(std::vector<Ort::Value>& output_tensors, const cv::Mat&
129131
{
130132
if (output_tensors.empty())
131133
{
132-
std::cerr << "[SAM]: Decoder returned no outputs." << std::endl;
134+
CONSOLE_BRIDGE_logError("[SAM]: Decoder returned no outputs.");
133135
return;
134136
}
135137

@@ -145,7 +147,7 @@ void Utils::PostProcess(std::vector<Ort::Value>& output_tensors, const cv::Mat&
145147

146148
if (masksIdx < 0)
147149
{
148-
std::cerr << "[SAM]: No 4D mask tensor found in decoder outputs." << std::endl;
150+
CONSOLE_BRIDGE_logError("[SAM]: No 4D mask tensor found in decoder outputs.");
149151
return;
150152
}
151153

@@ -241,6 +243,6 @@ void Utils::PostProcess(std::vector<Ort::Value>& output_tensors, const cv::Mat&
241243
}
242244
else
243245
{
244-
std::cerr << "[SAM]: Unexpected mask tensor shape." << std::endl;
246+
CONSOLE_BRIDGE_logError("[SAM]: Unexpected mask tensor shape.");
245247
}
246248
}

test/sam_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ TEST_F(SamInferenceTest, CreateSessionWithValidModel)
7171
TEST_F(SamInferenceTest, CreateSessionWithInvalidModel)
7272
{
7373
params_encoder.modelPath = "nonexistent_model.onnx";
74-
const char* result = samSegmentors[0]->CreateSession(params_encoder);
75-
EXPECT_NE(result, nullptr) << "CreateSession should fail with invalid model path";
74+
EXPECT_THROW(samSegmentors[0]->CreateSession(params_encoder), std::runtime_error)
75+
<< "CreateSession should throw an exception with invalid model path";
7676
}
7777

7878
// End-to-end check: with both encoder/decoder models present, the pipeline runs

0 commit comments

Comments
 (0)