[smart] fix possible memory leak (#7832)

Signed-off-by: Shell <smokewood@qq.com>
This commit is contained in:
Shell 2023-07-22 10:30:29 +08:00 committed by GitHub
parent f52095d228
commit 52d3637fba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -709,9 +709,11 @@ struct timer_obj
rt_uint32_t status;
int sigev_signo;
clockid_t clockid;
timer_t timer_id;
#ifdef RT_USING_SMART
pid_t pid;
struct rt_work *work;
rt_list_t lwp_node;
#endif
};
@ -759,6 +761,17 @@ static void _lwp_timer_event_from_pid(struct rt_work *work, void *param)
rt_free(work);
}
int timer_list_free(rt_list_t *timer_list)
{
struct timer_obj *pos, *n;
rt_list_for_each_entry_safe(pos, n, timer_list, lwp_node)
{
timer_delete(pos->timer_id);
}
return 0;
}
#endif /* RT_USING_SMART */
static void rtthread_timer_wrapper(void *timerobj)
@ -877,6 +890,7 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
if (lwp)
{
timer->pid = lwp_self()->pid;
rt_list_insert_after(&lwp->timer, &timer->lwp_node);
}
else
{
@ -903,6 +917,8 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
return -1; /* todo:memory leak */
}
_g_timerid[_timerid] = timer;
timer->timer_id = (timer_t)(rt_ubase_t)_timerid;
*timerid = (timer_t)(rt_ubase_t)_timerid;
return 0;
@ -948,6 +964,11 @@ int timer_delete(timer_t timerid)
}
rt_ktime_hrtimer_detach(&timer->hrtimer);
#ifdef RT_USING_SMART
if (timer->pid)
rt_list_remove(&timer->lwp_node);
#endif
rt_free(timer);
return 0;
}

View File

@ -106,6 +106,7 @@ struct rt_lwp
pid_t tty_old_pgrp;
pid_t session;
rt_list_t t_grp;
rt_list_t timer; /* POSIX timer object binding to a process */
int leader; /*boolean value for session group_leader*/
struct dfs_fdtable fdt;
@ -165,6 +166,9 @@ void lwp_user_setting_save(rt_thread_t thread);
void lwp_user_setting_restore(rt_thread_t thread);
int lwp_setaffinity(pid_t pid, int cpu);
/* ctime lwp API */
int timer_list_free(rt_list_t *timer_list);
#ifdef ARCH_MM_MMU
struct __pthread {
/* Part 1 -- these fields may be external or

View File

@ -327,6 +327,7 @@ struct rt_lwp* lwp_new(void)
lwp->session = -1;
lwp->tty = RT_NULL;
rt_list_init(&lwp->t_grp);
rt_list_init(&lwp->timer);
lwp_user_object_lock_init(lwp);
lwp->address_search_head = RT_NULL;
rt_wqueue_init(&lwp->wait_queue);
@ -502,6 +503,7 @@ void lwp_free(struct rt_lwp* lwp)
}
}
timer_list_free(&lwp->timer);
lwp_pid_put(lwp_to_pid(lwp));
rt_hw_interrupt_enable(level);
rt_free(lwp);