[Converter] Fix mem leak of the converter
authorGichan Jang <gichan2.jang@samsung.com>
Thu, 1 Apr 2021 10:08:18 +0000 (19:08 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 5 Apr 2021 06:41:05 +0000 (15:41 +0900)
A memory leak occurred because the name of the tensor was not released.
Free the name of the tensor and set default name as "" instead of
"Anonymous"

Signed-off-by: Gichan Jang <gichan2.jang@samsung.com>
ext/nnstreamer/extra/nnstreamer_protobuf.cc
ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc
ext/nnstreamer/tensor_converter/tensor_converter_flexbuf.cc
ext/nnstreamer/tensor_decoder/tensordec-flatbuf.cc
gst/nnstreamer/tensor_converter/tensor_converter.c

index 2b26404..08348d2 100644 (file)
@@ -69,7 +69,7 @@ gst_tensor_decoder_protobuf (const GstTensorsConfig *config,
 
     name = config->info.info[i].name;
     if (name == NULL) {
-      tensor->set_name ("Anonymous");
+      tensor->set_name ("");
     } else {
       tensor->set_name (name);
     }
@@ -144,7 +144,9 @@ gst_tensor_converter_protobuf (GstBuffer *in_buf, gsize *frame_size,
 
   for (guint i = 0; i < config->info.num_tensors; i++) {
     const nnstreamer::protobuf::Tensor *tensor = &tensors.tensor (i);
-    config->info.info[i].name = g_strdup (tensor->name ().c_str ());
+    const gchar *name = tensor->name ().c_str ();
+
+    config->info.info[i].name = (name && strlen (name) > 0) ? g_strdup (name) : NULL;
     config->info.info[i].type = (tensor_type)tensor->type ();
     for (guint j = 0; j < NNS_TENSOR_RANK_LIMIT; j++) {
       config->info.info[i].dimension[j] = tensor->dimension (j);
index b24dbf0..f4d0857 100644 (file)
@@ -125,8 +125,9 @@ fbc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensorsC
 
   for (guint i = 0; i < config->info.num_tensors; i++) {
     gsize offset;
+    const gchar *name = tensor->Get (i)->name ()->str ().c_str ();
 
-    config->info.info[i].name = g_strdup (tensor->Get (i)->name ()->str ().c_str ());
+    config->info.info[i].name = (name && strlen (name) > 0) ? g_strdup (name) : NULL;
     config->info.info[i].type = (tensor_type)tensor->Get (i)->type ();
     tensor_data = tensor->Get (i)->data ();
 
index 3863680..4dc6eeb 100644 (file)
@@ -142,7 +142,9 @@ flxc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensors
     gchar * tensor_key = g_strdup_printf ("tensor_%d", i);
     gsize offset;
     flexbuffers::Vector tensor = tensors[tensor_key].AsVector ();
-    config->info.info[i].name = g_strdup (tensor[0].AsString ().c_str ());
+    const gchar *name = tensor[0].AsString ().c_str ();
+
+    config->info.info[i].name = (name && strlen (name) > 0) ? g_strdup (name) : NULL;
     config->info.info[i].type = (tensor_type) tensor[1].AsInt32 ();
 
     flexbuffers::TypedVector dim = tensor[2].AsTypedVector ();
index 4935e5e..7e78d4c 100644 (file)
@@ -99,7 +99,7 @@ fbd_decode (void **pdata, const GstTensorsConfig *config,
     name = config->info.info[i].name;
 
     if (name == NULL)
-      tensor_name = builder.CreateString ("Anonymous");
+      tensor_name = builder.CreateString ("");
     else
       tensor_name = builder.CreateString (name);
 
index cbd00c0..d4f20e9 100644 (file)
@@ -178,7 +178,7 @@ static GstCaps *gst_tensor_converter_query_caps (GstTensorConverter * self,
     GstPad * pad, GstCaps * filter);
 static gboolean gst_tensor_converter_parse_caps (GstTensorConverter * self,
     const GstCaps * caps);
-static gboolean gst_tensor_converter_update_caps (GstTensorConverter * self,
+static void gst_tensor_converter_update_caps (GstTensorConverter * self,
     GstPad * pad, GstTensorsConfig * config);
 static const NNStreamerExternalConverter *findExternalConverter (const char
     *media_type_name);
@@ -584,8 +584,9 @@ gst_tensor_converter_sink_event (GstPad * pad, GstObject * parent,
 
       if (gst_tensor_converter_parse_caps (self, in_caps)) {
         gst_event_unref (event);
-        return gst_tensor_converter_update_caps (self, self->srcpad,
+        gst_tensor_converter_update_caps (self, self->srcpad,
             &self->tensors_config);
+        return TRUE;
       }
       break;
     }
@@ -1049,61 +1050,45 @@ gst_tensor_converter_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
       frames_in = buf_size / frame_size;
       break;
     case _NNS_MEDIA_ANY:
+    {
+      GstTensorsConfig new_config;
       if (self->mode == _CONVERTER_MODE_CUSTOM_CODE) {
-        GstTensorsConfig new_config;
         if (self->custom.func == NULL) {
           nns_loge
               ("custom condition of the tensor_converter is not configured.");
           return GST_FLOW_ERROR;
         }
         inbuf = self->custom.func (buf, self->custom.data, &new_config);
-        if (inbuf == NULL) {
-          nns_loge ("Failed to run custom tensor converter.");
-          gst_buffer_unref (buf);
-          return GST_FLOW_ERROR;
-        }
-        if (gst_tensors_config_is_equal (config, &new_config) != TRUE) {
-          if (gst_tensor_converter_update_caps (self, self->srcpad,
-                  &new_config) == TRUE) {
-            self->tensors_config = new_config;
-          } else {
-            gst_buffer_unref (buf);
-            return GST_FLOW_ERROR;
-          }
-        }
         frames_in = 1;
         frame_size = gst_buffer_get_size (inbuf);
-
-        gst_buffer_unref (buf);
-        break;
       } else if (self->externalConverter && self->externalConverter->convert) {
-        GstTensorsConfig new_config;
-
-        inbuf =
-            self->externalConverter->convert (buf, &frame_size, &frames_in,
+        inbuf = self->externalConverter->convert (buf, &frame_size, &frames_in,
             &new_config);
-
-        if (gst_tensors_config_is_equal (config, &new_config) != TRUE) {
-          if (gst_tensor_converter_update_caps (self, self->srcpad,
-                  &new_config) == TRUE) {
-            self->tensors_config = new_config;
-          } else {
-            return GST_FLOW_ERROR;
-          }
-        }
-
-        g_assert (inbuf != NULL);
-        g_assert (frame_size > 0);
-
-        if (inbuf != buf)
-          gst_buffer_unref (buf);
-
-        break;
       } else {
         GST_ERROR_OBJECT (self, "Undefined behavior with type %d\n",
             self->in_media_type);
         return GST_FLOW_NOT_SUPPORTED;
       }
+
+      if (inbuf == NULL) {
+        nns_loge ("Failed to convert media to tensors.");
+        gst_buffer_unref (buf);
+        gst_tensors_info_free (&new_config.info);
+        return GST_FLOW_ERROR;
+      }
+
+      if (!gst_tensors_config_is_equal (config, &new_config)) {
+        gst_tensor_converter_update_caps (self, self->srcpad, &new_config);
+        gst_tensors_info_free (&config->info);
+        *config = new_config;
+      } else {
+        gst_tensors_info_free (&new_config.info);
+      }
+
+      gst_buffer_unref (buf);
+
+      break;
+    }
     default:
       GST_ERROR_OBJECT (self, "Unsupported type %d\n", self->in_media_type);
       return GST_FLOW_ERROR;
@@ -1175,6 +1160,7 @@ gst_tensor_converter_reset (GstTensorConverter * self)
   }
 
   self->tensors_configured = FALSE;
+  gst_tensors_info_free (&self->tensors_config.info);
   gst_tensors_config_init (&self->tensors_config);
 
   self->have_segment = FALSE;
@@ -1878,13 +1864,13 @@ gst_tensor_converter_parse_caps (GstTensorConverter * self,
 }
 
 /**
- * @brief Update pas caps from tensors config
+ * @brief Update pad caps from tensors config
  */
-static gboolean
+static void
 gst_tensor_converter_update_caps (GstTensorConverter * self,
     GstPad * pad, GstTensorsConfig * config)
 {
-  GstCaps *curr_caps, *out_caps = NULL;
+  GstCaps *curr_caps, *out_caps;
 
   out_caps = gst_tensors_get_caps (pad, config);
 
@@ -1898,7 +1884,6 @@ gst_tensor_converter_update_caps (GstTensorConverter * self,
     gst_caps_unref (curr_caps);
 
   gst_caps_unref (out_caps);
-  return TRUE;
 }
 
 /**