Skip to content

Commit c371fbf

Browse files
committed
Replace g_assert with error handling
Release builds often define the compile option -DG_DISABLE_ASSERT for performance optimization. When this option is enabled, the g_assert(condition) macro is replaced by a no-op by the preprocessor. Signed-off-by: hyunil park <[email protected]>
1 parent b80c7f5 commit c371fbf

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

c/src/ml-api-common-tizen-feature-check.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,23 @@ static feature_info_s *feature_info = NULL;
4545
/**
4646
* @brief Internal function to initialize feature state.
4747
*/
48-
static void
48+
static int
4949
ml_tizen_initialize_feature_state (void)
5050
{
5151
int i;
5252

5353
if (feature_info == NULL) {
54-
feature_info = g_new0 (feature_info_s, 1);
55-
g_assert (feature_info);
54+
feature_info = g_try_new0 (feature_info_s, 1);
55+
if (feature_info == NULL) {
56+
_ml_loge ("Failed to allocate memory for feature_info");
57+
return ML_ERROR_OUT_OF_MEMORY;
58+
}
5659

5760
g_mutex_init (&feature_info->mutex);
5861
for (i = 0; i < ML_FEATURE_MAX; i++)
5962
feature_info->feature_state[i] = NOT_CHECKED_YET;
6063
}
64+
return ML_ERROR_NONE;
6165
}
6266

6367
/**
@@ -66,7 +70,12 @@ ml_tizen_initialize_feature_state (void)
6670
int
6771
_ml_tizen_set_feature_state (ml_feature_e ml_feature, int state)
6872
{
69-
ml_tizen_initialize_feature_state ();
73+
int status;
74+
75+
status = ml_tizen_initialize_feature_state ();
76+
if (status != ML_ERROR_NONE)
77+
return status;
78+
7079
g_mutex_lock (&feature_info->mutex);
7180

7281
/**
@@ -97,7 +106,9 @@ _ml_tizen_get_feature_enabled (ml_feature_e ml_feature)
97106
int ret;
98107
int feature_enabled;
99108

100-
ml_tizen_initialize_feature_state ();
109+
ret = ml_tizen_initialize_feature_state ();
110+
if (ret != ML_ERROR_NONE)
111+
return ret;
101112

102113
g_mutex_lock (&feature_info->mutex);
103114
feature_enabled = feature_info->feature_state[ml_feature];

c/src/ml-api-inference-pipeline.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ construct_pipeline_internal (const char *pipeline_description,
10311031
pipeline = gst_parse_launch (description, &err);
10321032
g_free (description);
10331033

1034-
if (pipeline == NULL || err) {
1034+
if (!GST_IS_PIPELINE (pipeline) || err) {
10351035
_ml_error_report
10361036
("ml_pipeline_construct error: gst_parse_launch cannot parse and launch the given pipeline = [%s]. The error message from gst_parse_launch is '%s'.",
10371037
pipeline_description, (err) ? err->message : "unknown reason");
@@ -1044,12 +1044,16 @@ construct_pipeline_internal (const char *pipeline_description,
10441044
goto failed;
10451045
}
10461046

1047-
g_assert (GST_IS_PIPELINE (pipeline));
10481047
pipe_h->element = pipeline;
10491048

10501049
/* bus and message callback */
10511050
pipe_h->bus = gst_element_get_bus (pipeline);
1052-
g_assert (pipe_h->bus);
1051+
if (pipe_h->bus == NULL) {
1052+
_ml_error_report
1053+
("ml_pipeline_construct error: Failed to retrieve bus from the pipeline.");
1054+
status = ML_ERROR_STREAMS_PIPE;
1055+
goto failed;
1056+
}
10531057

10541058
gst_bus_enable_sync_message_emission (pipe_h->bus);
10551059
pipe_h->signal_msg = g_signal_connect (pipe_h->bus, "sync-message",

java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ nns_attach_current_thread (pipeline_info_s * pipe_info)
2222
JavaVM *jvm;
2323
JavaVMAttachArgs args;
2424

25-
g_assert (pipe_info);
25+
if (!pipe_info) {
26+
_ml_loge ("pipe_info is NULL.");
27+
return NULL;
28+
}
29+
2630
jvm = pipe_info->jvm;
2731

2832
args.version = pipe_info->version;
@@ -48,7 +52,10 @@ nns_get_jni_env (pipeline_info_s * pipe_info)
4852
{
4953
JNIEnv *env;
5054

51-
g_assert (pipe_info);
55+
if (!pipe_info) {
56+
_ml_loge ("pipe_info is NULL.");
57+
return NULL;
58+
}
5259

5360
if ((env = pthread_getspecific (pipe_info->jni_env)) == NULL) {
5461
env = nns_attach_current_thread (pipe_info);

java/android/nnstreamer/src/main/jni/nnstreamer-native-common.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,13 @@ nnstreamer_native_get_data_path (void)
554554
char *data_path = NULL;
555555

556556
G_LOCK (nns_native_lock);
557-
g_assert (g_nns_is_initialized);
557+
558+
if (!g_nns_is_initialized) {
559+
_ml_loge ("NNStreamer native library is not initialized.");
560+
G_UNLOCK (nns_native_lock);
561+
return NULL;
562+
}
563+
558564
data_path = g_files_dir;
559565
G_UNLOCK (nns_native_lock);
560566

0 commit comments

Comments
 (0)