bremenpl wrote on Friday, December 21, 2018:
Hello there,
I am using FreeRTOS V9.0.0. the code is added at project generation in the CubeMX software from ST for their STM32 targets. In the project I am working on, I have noticed random, infrequent resets of the device (every couple hours, sometimes even days). The device resets itself on purpose, after entering a critical section. Please consider this function, which is a malloc wrapper (I am using heap4):
/**
* @brief FreeRTOS \ref pvPortMalloc wrapper with logging.
* @param size: amount of bytes to allocate on heap.
* @return non zero if allocation was successful.
*/
void* util_malloc(const size_t size)
{
assert_param(size);
// saved before malloc is called
const size_t freeHeap = xPortGetFreeHeapSize();
void* ptr = pvPortMalloc(size);
const size_t freeHeapAfter = xPortGetFreeHeapSize();
if (freeHeapAfter >= freeHeap)
{
log_PushLine(e_logLevel_Critical,
"Failed to allocate heap. pre %u, after %u, needed %u",
freeHeap, freeHeapAfter, size);
// program counter wont reach here, since Critical log kills the thread
ptr = NULL;
}
return ptr;
}
In those rare situations, the logs reveal such traces:
[2018-11-27T13:34:14.761Z][tUartRx ][001]<Criti>: Failed to allocate heap. pre 2776, after 4160556831, needed 1
[2018-11-28T06:05:55.698Z][tUartRx ][001]<Criti>: Failed to allocate heap. pre 2784, after 4160556839, needed 6
[2018-11-28T09:32:50.874Z][tUartRx ][001]<Criti>: Failed to allocate heap. pre 2776, after 4160556831, needed 1
[2018-11-29T00:37:42.917Z][tNmMain ][005]<Criti>: Failed to allocate heap. pre 2904, after 4160556959, needed 17
[2018-11-29T00:37:44.494Z][tUartRx ][001]<Criti>: Failed to allocate heap. pre 4160561167, after 4160561167, needed 144
etc
I cannot understand where does the free heap value after allocation come from. I would appreciate all help regarding debugging this issue.