diff options
| author | qinpan1003 <[email protected]> | 2019-10-10 22:42:14 +0800 |
|---|---|---|
| committer | qinpan1003 <[email protected]> | 2019-10-10 22:42:14 +0800 |
| commit | b838280d248f1525d617955819e513a1f059a3ec (patch) | |
| tree | bbc89a3ec32f2e5a20a1658bbf06411976b623a0 /components | |
| parent | e3e24e8f751f8d18cef4a99594759e1cd97e9879 (diff) | |
fix bug for pthread_create memory leak
Diffstat (limited to 'components')
| -rwxr-xr-x[-rw-r--r--] | components/libc/pthreads/pthread.c | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c index 7ecf304038..5d3bc0796a 100644..100755 --- 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, |
