STM32L4 hardfault error with FreeRTOS after a couple of hours

The malloc locks make malloc thread safe and are required if using malloc (used by newlib) in a multi threaded environment (FreeRTOS). You’ll want to implement __malloc_lock() and __malloc_unlock(). A common implementation is to call vTaskSuspendAll() for the lock function and to call vTaskResumeAll() for the unlock.

However, if you are using printf() from multiple tasks, and if printf() is directed to a UART that uses a driver that isn’t thread safe, that might be causing the corruption / hard faults. Can you test by removing all the printf() statements?