cancel
Showing results for 
Search instead for 
Did you mean: 

RTOS f_mount() Heap Leak in HEAP_1 model

mantisrobot
Associate II

I have a 1 second loop that tries to mount the SD card, It repeatedly calls f_mount() until an SD is detected, however, I've noticed that there is a semaphore created each time f_mount() is called which is not deleted in HEAP_1 mode. So after some time all the heap is consumed. I realise that using HEAP_1 mode objects aren't freed, so should I be doing this another way if I want to mount the disk if its ejected and re-inserted?

This is the call the creates the smaphore:

#if _FS_REENTRANT
/*------------------------------------------------------------------------*/
/* Create a Synchronization Object                                        */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to create a new
/  synchronization object, such as semaphore and mutex. When a 0 is returned,
/  the f_mount() function fails with FR_INT_ERR.
*/
 
int ff_cre_syncobj (	/* 1:Function succeeded, 0:Could not create the sync object */
	BYTE vol,			/* Corresponding volume (logical drive number) */
	_SYNC_t *sobj		/* Pointer to return the created sync object */
)
{
 
    int ret;
#if _USE_MUTEX
 
#if (osCMSIS < 0x20000U)
    osMutexDef(MTX);
    *sobj = osMutexCreate(osMutex(MTX));
#else
    *sobj = osMutexNew(NULL);
#endif
 
#else
 
#if (osCMSIS < 0x20000U)
    osSemaphoreDef(SEM);
    *sobj = osSemaphoreCreate(osSemaphore(SEM), 1);
#else
    *sobj = osSemaphoreNew(1, 1, NULL);
#endif
 
#endif
    ret = (*sobj != NULL);
 
    return ret;
}

And this is the code that calls osSemaphoreDelete() which is ignored in HEAP_1 mode:

/*------------------------------------------------------------------------*/
/* Delete a Synchronization Object                                        */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to delete a synchronization
/  object that created with ff_cre_syncobj() function. When a 0 is returned,
/  the f_mount() function fails with FR_INT_ERR.
*/
 
int ff_del_syncobj (	/* 1:Function succeeded, 0:Could not delete due to any error */
	_SYNC_t sobj		/* Sync object tied to the logical drive to be deleted */
)
{
#if _USE_MUTEX
    osMutexDelete (sobj);
#else
    osSemaphoreDelete (sobj);
#endif
    return 1;
}
 
 
osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id) {
  SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
  osStatus_t stat;
 
#ifndef USE_FreeRTOS_HEAP_1
  if (IS_IRQ()) {
    stat = osErrorISR;
  }
  else if (hSemaphore == NULL) {
    stat = osErrorParameter;
  }
  else {
    #if (configQUEUE_REGISTRY_SIZE > 0)
    vQueueUnregisterQueue (hSemaphore);
    #endif
 
    stat = osOK;
    vSemaphoreDelete (hSemaphore);
  }
#else
  stat = osError;
#endif
 
  return (stat);
}

0 REPLIES 0