From baeb5af9a4d8b34c39edd2a5d72abf7661e7eb9a Mon Sep 17 00:00:00 2001 From: Reda Maher <52288047+RedaMaher@users.noreply.github.com> Date: Tue, 29 Sep 2020 23:06:10 +0200 Subject: [PATCH] Posix: Free the allocated memory after deleting a task or ending the scheduler (#181) * Posix: Free Idle task resources after ending the scheduler In case of using Posix simulator and ending the scheduler, it does not free the resources allocated by the idle task. This causes the memory checkers (Valgrind, Address Sanitizers, ..) to complain. * Posix: Free the condition variable memory in the correct place In case of deleting a task from another task, the deletion happens immediately and the thread is canceled but the memory allocated by the task condition variable is not freed. This causes the memory checkers (Valgrind, Address sanitizers, ..) to complain. * Posix: End Timer thread and free its resources after ending the scheduler --- portable/ThirdParty/GCC/Posix/port.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/portable/ThirdParty/GCC/Posix/port.c b/portable/ThirdParty/GCC/Posix/port.c index 086ad487a..7d7b5d887 100644 --- a/portable/ThirdParty/GCC/Posix/port.c +++ b/portable/ThirdParty/GCC/Posix/port.c @@ -62,6 +62,7 @@ /* Scheduler includes. */ #include "FreeRTOS.h" #include "task.h" +#include "timers.h" #include "utils/wait_for_event.h" /*-----------------------------------------------------------*/ @@ -197,6 +198,13 @@ sigset_t xSignals; sigwait( &xSignals, &iSignal ); } + /* Cancel the Idle task and free its resources */ + vPortCancelThread( xTaskGetIdleTaskHandle() ); +#if ( configUSE_TIMERS == 1 ) + /* Cancel the Timer task and free its resources */ + vPortCancelThread( xTimerGetTimerDaemonTaskHandle() ); +#endif /* configUSE_TIMERS */ + /* Restore original signal mask. */ (void)pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL ); @@ -405,6 +413,7 @@ Thread_t *pxThreadToCancel = prvGetThreadFromTask( pxTaskToDelete ); */ pthread_cancel( pxThreadToCancel->pthread ); pthread_join( pxThreadToCancel->pthread, NULL ); + event_delete( pxThreadToCancel->ev ); } /*-----------------------------------------------------------*/ @@ -444,7 +453,6 @@ BaseType_t uxSavedCriticalNesting; prvResumeThread( pxThreadToResume ); if ( pxThreadToSuspend->xDying ) { - event_delete(pxThreadToSuspend->ev); pthread_exit( NULL ); } prvSuspendSelf( pxThreadToSuspend );