From eb168d32053f9204336c37af98210810957cedd2 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Fri, 10 Jun 2022 04:10:39 +0100
Subject: [PATCH 18/30] fix memory leak of remove layer

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 .../image/oci/storage/layer_store/layer_store.c       | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
index bb9e5b94..7e95a52f 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
@@ -213,7 +213,7 @@ static bool append_layer_into_list(layer_t *l)
 }
 
 // only delete item from list, free item->elem by caller
-static inline void delete_g_layer_list_item(struct linked_list *item)
+static inline void delete_g_layer_list_item(struct linked_list *item, bool rm_val)
 {
     if (item == NULL) {
         return;
@@ -221,6 +221,11 @@ static inline void delete_g_layer_list_item(struct linked_list *item)
 
     linked_list_del(item);
 
+    if (rm_val) {
+        layer_ref_dec((layer_t *)item->elem);
+        item->elem = NULL;
+    }
+
     free(item);
     g_metadata.layers_list_len -= 1;
 }
@@ -235,7 +240,7 @@ void remove_layer_list_tail()
 
     item = g_metadata.layers_list.prev;
 
-    delete_g_layer_list_item(item);
+    delete_g_layer_list_item(item, false);
 }
 
 static bool init_from_conf(const struct storage_module_init_options *conf)
@@ -725,7 +730,7 @@ static int remove_memory_stores(const char *id)
         if (strcmp(tl->slayer->id, id) != 0) {
             continue;
         }
-        delete_g_layer_list_item(item);
+        delete_g_layer_list_item(item, true);
         break;
     }
 
-- 
2.32.1 (Apple Git-133)

