vApplicationMallocFailedHook when using queues


I have got STM32F429, CubeIDE and FreeRTOS
I use UART, Ethernet(netconn API), SDIO

#define configMINIMAL_STACK_SIZE                (4096)
#define configTOTAL_HEAP_SIZE                   65536
#define TCP_MSS 4096
#define DEFAULT_TCP_RECVMBOX_SIZE       2048

I use appropriate queues to each peripheral
I have got the following queue structure

typedef struct MesNode_
  char* message;
  size_t size;
} MesNode;

I have got the following buffers

#define BUFFER_SIZE 4096
#define Middle_BUFFER_SIZE 1024
char uartBuffer[BUFFER_SIZE];
char TCPBuffer[BUFFER_SIZE];

Im tryng to use queues with circular with the following circular buffer

MesNode make_msg_circ(const char* buffer, const char* begin, const char* end)
  MesNode mNode;
  mNode.size = (end > begin) ? end - begin: BUFFER_SIZE - (begin - end);
  mNode.message = pvPortMalloc(msgNode.size + 1);
  if (end > begin) {
    memcpy(msgNode.message, begin, msgNode.size);
  else {
    size_t tailSize = buffer + BUFFER_SIZE - begin;
    size_t headSize = end - buffer;
    memcpy(msgNode.message, begin, tailSize);
    memcpy(msgNode.message + tailSize, buffer, headSize);
  mNode.message[msgNode.size] = '\0';
  return mNode;

I applicate buffer in the following function of data processing. What function get last adress of pointer in buffer

typedef void (*process_fa)(MesNode* mNode);

char* data_process(osThreadId_t thread, char* beginBuffer)
  DMA_HandleTypeDef* dmaRx;
  char* uartBuffer;
  process_fa process_func;
  find_circ_fa find_func;
  uint16_t dmaRxv;

  if (thread == uartHandle) {
    dmaRx = dmaRxPpm;
    uartBuffer = uartPpmBuffer;
    process_func = data_process_ppm;
    find_func = find_circ_end_lsn;
  else if (thread == TCPTxHandle) {
	  dmaRx = dmaRxPpm;
		  uartBuffer = uartPpmBuffer;
		  process_func = data_process_ttcp;
		  find_func = find_circ_end_lsn;
		  uartBuffer = TCPBuffer;
		  dmaRxv = 	rx_size;
		  process_func = data_process_ftcp2;
		  find_func = find_circ_end_lsn;//find_circ_end_line;

  else if (thread == TCPProcHandle) {
	  	  dmaRxv = lenc;//dmaTCP.length;
	      uartBuffer = TCPBuffer;

	      process_func = data_process_ftcp;
	      find_func = find_circ_end_line;


  else {
    return beginBuffer;

  size_t dmaRxSize;
	  dmaRxSize = BUFFER_SIZE - dmaRxv;
	  dmaRxSize = BUFFER_SIZE - dmaRx->Instance->NDTR;
  char* endRxDmaBuffer = uartBuffer + dmaRxSize;
  size_t lenRxData = (endRxDmaBuffer >= beginBuffer) ? endRxDmaBuffer - beginBuffer:
      dmaRxSize + uartBuffer + BUFFER_SIZE - beginBuffer;

  char* startPosition = beginBuffer;
  char* endPosition;
  char* startFind = beginBuffer;
  size_t lenTail = lenRxData;

  while (1) {
    endPosition = find_func(uartBuffer, startFind, lenTail);
    if (endPosition) {
      MesNode mNode = make_msg_circ(uartBuffer, startPosition, endPosition);
      lenTail -= mNode.size;
      startPosition = endPosition;
      startFind = endPosition;
  return startFind;


I suppose what portmalloc problem cause definition of mNode.size but still found the reason
Can anybody help to clarify this moment?

I operate the 1K data and tranfer it

So what’s your actual problem ? Is vApplicationMallocFailedHook invoked and you don’t know why ?
Did you verify that the size calculation is always right and you have sufficient heap available ?
Is the heap exhausted later on perhaps due to a memory leak somewhere else ?
Which heap implementation do you use ?

BTW are you sure this configuration is right ?

With the default/standard MTU size 1500 the MSS is 1460 (MTU - IP/TCP header size).

Thank you for your replay!
I use heap4 and increased TCP_MSS due to dividied meassage during transfer
Yes I stopped after about 5 min of transfer in debug I stopped in`vApplicationMallocFailedHook. I see what msg.size not stable in debug but cant find it in code

Also maybe heap leak cause of netconn API using

Pls help!

Not sure what you mean with divided message. It’s TCP and this is a stream protocol i.e. there is no such thing as a message or datagram known from UDP.
A receiver needs to take care to assemble an application layer message correctly according to your application protocol (sending messages).
Regarding the memory leak I afraid no one can help you … it’s up to you to find the reason/bug in your code where you forgot to free up buffers or messages after use.
Good luck !