Concern on the usage of malloc() on USB Host MSC driver library
I have concern on the usage of malloc() in the generated USB Host MSC library.
In file usbh_msc.c, take a look at the usage of USBH_malloc)_:
static USBH_StatusTypeDef USBH_MSC_InterfaceInit(USBH_HandleTypeDef *phost)
{
.............
MSC_HandleTypeDef *MSC_Handle;
.........
phost->pActiveClass->pData = (MSC_HandleTypeDef *)USBH_malloc(sizeof(MSC_HandleTypeDef));
MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
if (MSC_Handle == NULL)
{
USBH_DbgLog("Cannot allocate memory for MSC Handle");
return USBH_FAIL;
}
}where it is defined as following on usbh_conf.h:
/** Alias for memory allocation. */
#define USBH_malloc mallocMy questions:
- Why it uses dynamic location malloc()? To my understanding for real time application this is risky.
- Why currently the malloc() return NULL memory allocation? I have defined my heap size 0x2000 in the CubeMX project config which should suffice to serve the requsted 256 byte (i.e. sizeof(MSC_HandleTypeDef)).
- Why in the code, I never see this memory freed? It would lead to potential memory leak right?
