Skip to content

Commit 8e6ec4d

Browse files
authored
Merge pull request #3124 from qinpan1003/pthreads
fix bug for pthread_create memory leak
2 parents e3e24e8 + b838280 commit 8e6ec4d

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

components/libc/pthreads/pthread.c

100644100755
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void _pthread_data_destroy(pthread_t pth)
102102
rt_sem_delete(ptd->joinable_sem);
103103

104104
/* release thread resource */
105-
if (ptd->attr.stackaddr == RT_NULL)
105+
if (ptd->attr.stackaddr == RT_NULL && ptd->tid->stack_addr != RT_NULL)
106106
{
107107
/* release thread allocated stack */
108108
rt_free(ptd->tid->stack_addr);
@@ -219,20 +219,6 @@ int pthread_create(pthread_t *pid,
219219
}
220220

221221
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
222-
if (ptd->attr.stackaddr == 0)
223-
{
224-
stack = (void *)rt_malloc(ptd->attr.stacksize);
225-
}
226-
else
227-
{
228-
stack = (void *)(ptd->attr.stackaddr);
229-
}
230-
231-
if (stack == RT_NULL)
232-
{
233-
ret = ENOMEM;
234-
goto __exit;
235-
}
236222

237223
/* pthread is a static thread object */
238224
ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
@@ -241,6 +227,7 @@ int pthread_create(pthread_t *pid,
241227
ret = ENOMEM;
242228
goto __exit;
243229
}
230+
memset(ptd->tid, 0, sizeof(struct rt_thread));
244231

245232
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
246233
{
@@ -260,6 +247,22 @@ int pthread_create(pthread_t *pid,
260247
ptd->thread_entry = start;
261248
ptd->thread_parameter = parameter;
262249

250+
/* stack */
251+
if (ptd->attr.stackaddr == 0)
252+
{
253+
stack = (void *)rt_malloc(ptd->attr.stacksize);
254+
}
255+
else
256+
{
257+
stack = (void *)(ptd->attr.stackaddr);
258+
}
259+
260+
if (stack == RT_NULL)
261+
{
262+
ret = ENOMEM;
263+
goto __exit;
264+
}
265+
263266
/* initial this pthread to system */
264267
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
265268
stack, ptd->attr.stacksize,

0 commit comments

Comments
 (0)