Memory leak when i call FLEXCAN_DRV_Init to initialize the can controller

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Memory leak when i call FLEXCAN_DRV_Init to initialize the can controller

1,690 Views
cheney
Contributor III

Hi, When i use the can driver, i found that if there's no protect in the creation of semaphore.

When the CAN bus enter busoff state, i use FLEXCAN_DRV_Init to initialize the can controller. But when FLEXCAN_DRV_Init is called, it will call OSIF_SemaCreate((semaphore_t *const)&state->mbs[i].mbSema, 0U)  to create the semaphore, no matter the semaphore is created or not.

So what should i do in this case?

0 Kudos
5 Replies

1,176 Views
alexandrunan
NXP Employee
NXP Employee

Hello, On which platform did you observed this behavior, the release version you used  ?

Did you run as baremetal or FreeRTOS, OSIF has different behavior ?

In case of bare metal :

The semaphores space should  already be allocated before you call FLEXCAN_DRV_Init in the flexcan_state_t  structure that is passed to the init function.

In the driver init function the semaphore is only enabled(initialized) by OSIF_SemaCreate((semaphore_t *const)&state->mbs[i].mbSema, 0U); 

0 Kudos

1,176 Views
cheney
Contributor III

Hi, Alexandru, thank you for you reply.

The development enviroment is showed below:

MCU:S32k144

IDE:S32DS v2

SDK:0.8.4

FreeRTOS: Used

Because the rtos is used, the OSIF_SemaCreate() is ported to RTOS API xSemaphoreCreateCounting().  Whenever i call FLEXCAN_DRV_Init, it will call OSIF_SemaCreate((semaphore_t *const)&state->mbs[i].mbSema, 0U) to create the semaphore and it will consume the memory size from the freertos heap.This will cause the memory leak.

0 Kudos

1,176 Views
AnaAldescu
NXP Employee
NXP Employee

Hello,

All semaphores used by FlexCAN are destroyed when calling FLEXCAN_DRV_Deinit. There should not be two subsequent calls of FLEXCAN_DRV_Init without calling FLEXCAN_DRV_Deinit in the mean time. Though, the driver should report an error status if FLEXCAN_DRV_Init is called twice. We will investigate this scenario and thank you for reporting this issue.

Best regards,

Ana

0 Kudos

1,176 Views
cheney
Contributor III

Hi, Ana.

Maybe we can add a judgement when creating the semaphore. If the semaphore is created, we can ignore the following creating process.

For example:

status_t FLEXCAN_DRV_Init(
uint8_t instance,
flexcan_state_t *state,
const flexcan_user_config_t *data)
{
...
   
for (i = 0; i < FEATURE_CAN_MAX_MB_NUM; i++)
{
if (state->mbSema_created == False)
{
osifStat = OSIF_SemaCreate((semaphore_t * const)&state->mbs[i].mbSema, 0U);
if (osifStat != STATUS_SUCCESS)
{
for (j = 0; j < i; j++)
{
(void)OSIF_SemaDestroy((semaphore_t * const)&state->mbs[j].mbSema);
}
return STATUS_ERROR;
}
}

state->mbs[i].isBlocking = false;
state->mbs[i].mb_message = NULL;
state->mbs[i].state = FLEXCAN_MB_IDLE;
}

state->mbSema_created = True;
...
}
status_t FLEXCAN_DRV_Deinit(uint8_t instance)
{
...
if(state->mbSema_created == True)
{
for (i = 0; i < FEATURE_CAN_MAX_MB_NUM; i++)
{
osifStat = OSIF_SemaDestroy((semaphore_t * const)&state->mbs[i].mbSema);
if (osifStat != STATUS_SUCCESS)
{
result = STATUS_ERROR;
}
}
}

state->mbSema_created = False;
...
}
0 Kudos

1,176 Views
alexandrunan
NXP Employee
NXP Employee

In this case, if the driver fails to initialize, return code FLEXCAN_DRV_Init is different from STATUS_SUCCESS, you can call FLEXCAN_DRV_Deinit that will destroy the already allocated Semaphores. In this case you can avoid excessive allocations.

0 Kudos