From b838280d248f1525d617955819e513a1f059a3ec Mon Sep 17 00:00:00 2001 From: qinpan1003 Date: Thu, 10 Oct 2019 22:42:14 +0800 Subject: [PATCH] fix bug for pthread_create memory leak --- components/libc/pthreads/pthread.c | 33 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) mode change 100644 => 100755 components/libc/pthreads/pthread.c diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c old mode 100644 new mode 100755 index 7ecf304038..5d3bc0796a --- a/components/libc/pthreads/pthread.c +++ b/components/libc/pthreads/pthread.c @@ -102,7 +102,7 @@ void _pthread_data_destroy(pthread_t pth) rt_sem_delete(ptd->joinable_sem); /* release thread resource */ - if (ptd->attr.stackaddr == RT_NULL) + if (ptd->attr.stackaddr == RT_NULL && ptd->tid->stack_addr != RT_NULL) { /* release thread allocated stack */ rt_free(ptd->tid->stack_addr); @@ -219,20 +219,6 @@ int pthread_create(pthread_t *pid, } rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++); - if (ptd->attr.stackaddr == 0) - { - stack = (void *)rt_malloc(ptd->attr.stacksize); - } - else - { - stack = (void *)(ptd->attr.stackaddr); - } - - if (stack == RT_NULL) - { - ret = ENOMEM; - goto __exit; - } /* pthread is a static thread object */ ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread)); @@ -241,6 +227,7 @@ int pthread_create(pthread_t *pid, ret = ENOMEM; goto __exit; } + memset(ptd->tid, 0, sizeof(struct rt_thread)); if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE) { @@ -260,6 +247,22 @@ int pthread_create(pthread_t *pid, ptd->thread_entry = start; ptd->thread_parameter = parameter; + /* stack */ + if (ptd->attr.stackaddr == 0) + { + stack = (void *)rt_malloc(ptd->attr.stacksize); + } + else + { + stack = (void *)(ptd->attr.stackaddr); + } + + if (stack == RT_NULL) + { + ret = ENOMEM; + goto __exit; + } + /* initial this pthread to system */ if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd, stack, ptd->attr.stacksize,