[Queue] fix mem leak case
authorJaeyun <jy1210.jung@samsung.com>
Tue, 1 Nov 2022 02:25:54 +0000 (11:25 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Thu, 3 Nov 2022 01:39:04 +0000 (10:39 +0900)
Code clean,
1. fix mem leak case - max buffer in queue.
2. use common destroy callback.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
src/libnnstreamer-edge/nnstreamer-edge-queue.c
src/libnnstreamer-edge/nnstreamer-edge-queue.h

index d96f26ef2108ede2e99316f9c00acf7f479ee5c9..46808881868ee697ea8a015e6585bef4dda85ece 100644 (file)
@@ -25,7 +25,7 @@ typedef struct _nns_edge_queue_data_s nns_edge_queue_data_s;
 struct _nns_edge_queue_data_s
 {
   void *data;
-  nns_edge_queue_data_destroy_cb destroy;
+  nns_edge_data_destroy_cb destroy;
   nns_edge_queue_data_s *next;
 };
 
@@ -107,7 +107,6 @@ bool
 nns_edge_queue_destroy (nns_edge_queue_h handle)
 {
   nns_edge_queue_s *q = (nns_edge_queue_s *) handle;
-  nns_edge_queue_data_s *qdata;
 
   if (!q) {
     nns_edge_loge ("[Queue] Invalid param, queue is null.");
@@ -116,16 +115,10 @@ nns_edge_queue_destroy (nns_edge_queue_h handle)
 
   nns_edge_lock (q);
   nns_edge_cond_signal (q);
-  while ((qdata = q->head) != NULL) {
-    if (qdata->destroy)
-      qdata->destroy (qdata->data);
 
-    q->head = qdata->next;
-    free (qdata);
-  }
+  while (q->length > 0U)
+    _pop_data (q, true);
 
-  q->head = q->tail = NULL;
-  q->length = 0U;
   nns_edge_unlock (q);
 
   nns_edge_cond_destroy (q);
@@ -184,7 +177,7 @@ nns_edge_queue_set_limit (nns_edge_queue_h handle, unsigned int limit,
  */
 bool
 nns_edge_queue_push (nns_edge_queue_h handle, void *data,
-    nns_edge_queue_data_destroy_cb destroy)
+    nns_edge_data_destroy_cb destroy)
 {
   nns_edge_queue_s *q = (nns_edge_queue_s *) handle;
   nns_edge_queue_data_s *qdata;
@@ -200,15 +193,6 @@ nns_edge_queue_push (nns_edge_queue_h handle, void *data,
     return false;
   }
 
-  qdata = calloc (1, sizeof (nns_edge_queue_data_s));
-  if (!qdata) {
-    nns_edge_loge ("[Queue] Failed to allocate new memory for data.");
-    return false;
-  }
-
-  qdata->data = data;
-  qdata->destroy = destroy;
-
   nns_edge_lock (q);
   if (q->max_data > 0U && q->length >= q->max_data) {
     /* Clear old data in queue if leaky option is 'old'. */
@@ -221,6 +205,15 @@ nns_edge_queue_push (nns_edge_queue_h handle, void *data,
     }
   }
 
+  qdata = calloc (1, sizeof (nns_edge_queue_data_s));
+  if (!qdata) {
+    nns_edge_loge ("[Queue] Failed to allocate new memory for data.");
+    goto done;
+  }
+
+  qdata->data = data;
+  qdata->destroy = destroy;
+
   if (!q->head)
     q->head = qdata;
   if (q->tail)
index 2a480642bc7a175db4c9ca8d273ce583496ddf2e..176ccdf4e6e195581d9bae5b0bc40c048b8c37cd 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <stdbool.h>
 #include <sys/time.h>
+#include "nnstreamer-edge.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -23,11 +24,6 @@ extern "C" {
 
 typedef void *nns_edge_queue_h;
 
-/**
- * @brief Callback called when data in queue is released.
- */
-typedef void (*nns_edge_queue_data_destroy_cb) (void *data);
-
 /**
  * @brief Enumeration for the queue leaky option.
  */
@@ -74,7 +70,7 @@ bool nns_edge_queue_set_limit (nns_edge_queue_h handle, unsigned int limit, nns_
  * @param[in] destroy Nullable, the callback function to release data.
  * @return true on success.
  */
-bool nns_edge_queue_push (nns_edge_queue_h handle, void *data, nns_edge_queue_data_destroy_cb destroy);
+bool nns_edge_queue_push (nns_edge_queue_h handle, void *data, nns_edge_data_destroy_cb destroy);
 
 /**
  * @brief Remove and return the first data in queue.