Merge pull request #3124 from qinpan1003/pthreads

fix bug for pthread_create memory leak
This commit is contained in:
Bernard Xiong 2019-10-11 08:20:19 +08:00 committed by GitHub
commit 8e6ec4df83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 15 deletions

33
components/libc/pthreads/pthread.c Normal file → Executable file
View File

@ -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,