[fixes] Add bug fixes found with address sanitizer
authorParichay Kapoor <pk.kapoor@samsung.com>
Thu, 12 Aug 2021 06:44:25 +0000 (15:44 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Fri, 13 Aug 2021 02:24:09 +0000 (11:24 +0900)
This patch adds bug fixes after adding fixes related to address
sanitzer:
- NNStreamerLayer wasnt freeing resources upon destruction
- accessing released model handle in capi unittest
- not releasing model handle before end of unittest

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
nntrainer/layers/nnstreamer_layer.cpp
nntrainer/layers/nnstreamer_layer.h
test/tizen_capi/unittest_tizen_capi.cpp

index 2c9d43cc40b4d58a40997818e81951faf39866f5..4719d24ad4487bda89bf3b40c8787844427f03b7 100644 (file)
@@ -62,18 +62,9 @@ int NNStreamerLayer::nnst_info_to_tensor_dim(ml_tensors_info_h &out_res,
   return status;
 }
 
-NNStreamerLayer::~NNStreamerLayer() {
-  try {
-    finalizeError(ML_ERROR_NONE);
-  } catch (std::exception &e) {
-    std::cerr << "failed in destructor, reason: " << e.what();
-  }
-}
-
-void NNStreamerLayer::finalizeError(int status) {
-  if (status == ML_ERROR_NONE)
-    return;
+NNStreamerLayer::~NNStreamerLayer() { release(); }
 
+void NNStreamerLayer::release() noexcept {
   if (in_res) {
     ml_tensors_info_destroy(in_res);
     in_res = nullptr;
@@ -98,6 +89,13 @@ void NNStreamerLayer::finalizeError(int status) {
     ml_single_close(single);
     single = nullptr;
   }
+}
+
+void NNStreamerLayer::finalizeError(int status) {
+  if (status == ML_ERROR_NONE)
+    return;
+
+  release();
 
   if (status != ML_ERROR_NONE)
     throw std::invalid_argument(
index 6af829ad8353ac1506c25e6220dcdbefe1cbafbf..872d35a12d2e63919e4f2de787b704e4ecab5ddb 100644 (file)
@@ -98,6 +98,11 @@ private:
    */
   void finalizeError(int status);
 
+  /**
+   * @brief     release the layer resources
+   */
+  void release() noexcept;
+
   /**
    * @brief    convert nnstreamer's tensor_info to nntrainer's tensor_dim
    * @param[in] out_res nnstreamer's tensor_info
index b399136900cb1d24892407b67e3eb036ad8b78b1..10ebb36c4ab0191a594e81edb934c4c71a74f5ec 100644 (file)
@@ -1085,6 +1085,9 @@ TEST(nntrainer_capi_nnmodel, get_input_output_dimension_03_n) {
             ML_ERROR_INVALID_PARAMETER);
   EXPECT_EQ(ml_train_model_get_output_tensors_info(handle, &output_info),
             ML_ERROR_INVALID_PARAMETER);
+
+  status = ml_train_model_destroy(handle);
+  EXPECT_EQ(status, ML_ERROR_NONE);
 }
 
 TEST(nntrainer_capi_nnmodel, get_input_output_dimension_04_n) {
@@ -1112,6 +1115,7 @@ TEST(nntrainer_capi_nnmodel, get_input_output_dimension_05_n) {
 
   status = ml_train_model_destroy(handle);
   EXPECT_EQ(status, ML_ERROR_NONE);
+  handle = NULL;
 
   EXPECT_EQ(ml_train_model_get_input_tensors_info(handle, &input_info),
             ML_ERROR_INVALID_PARAMETER);