From a305edd4db9b1c7d1706fb717bcd9923775afed4 Mon Sep 17 00:00:00 2001 From: liuxianliang Date: Tue, 5 Jul 2022 10:36:53 +0800 Subject: [PATCH] [fix] memory leak when the value of posix semaphore is zero. --- components/libc/posix/ipc/semaphore.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/components/libc/posix/ipc/semaphore.c b/components/libc/posix/ipc/semaphore.c index d5b1a70279..6a4ae51e57 100644 --- a/components/libc/posix/ipc/semaphore.c +++ b/components/libc/posix/ipc/semaphore.c @@ -39,7 +39,9 @@ static void posix_sem_delete(sem_t *psem) posix_sem_list = psem->next; rt_sem_delete(psem->sem); - rt_free(psem); + + if(psem->unamed == 0) + rt_free(psem); return; } @@ -55,7 +57,9 @@ static void posix_sem_delete(sem_t *psem) /* delete RT-Thread mqueue */ rt_sem_delete(psem->sem); - rt_free(psem); + + if(psem->unamed == 0) + rt_free(psem); return ; } @@ -107,8 +111,6 @@ RTM_EXPORT(sem_close); int sem_destroy(sem_t *sem) { - rt_err_t result; - if ((!sem) || !(sem->unamed)) { rt_set_errno(EINVAL); @@ -118,8 +120,7 @@ int sem_destroy(sem_t *sem) /* lock posix semaphore list */ rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER); - result = rt_sem_trytake(sem->sem); - if (result != RT_EOK) + if(rt_list_len(&sem->sem->parent.suspend_thread) != 0) { rt_sem_release(&posix_sem_lock); rt_set_errno(EBUSY);