# vApplicationMallocFailedHook when using queues

**URL:** <https://forums.freertos.org/t/vapplicationmallocfailedhook-when-using-queues/13524>\
**Category:** Kernel\
**Created:** [September 23, 2021, 5:01am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-when-using-queues/13524 "2021-09-23T05:01:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![addi](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@addi](https://forums.freertos.org/u/addi)\
**Post date:** [September 23, 2021, 5:01am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-when-using-queues/13524/1 "2021-09-23T05:01:36Z")

</div>

Hello!

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

```auto
#define configMINIMAL_STACK_SIZE (4096)
#define configTOTAL_HEAP_SIZE 65536
#define TCPIP_THREAD_STACKSIZE 4096
#define DEFAULT_THREAD_STACKSIZE 4096
#define TCP_MSS 4096
#define DEFAULT_TCP_RECVMBOX_SIZE 2048
#define INTERFACE_THREAD_STACK_SIZE (4096)

```

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

```auto
typedef struct MesNode_
{  
  char* message;
  size_t size;
} MesNode;

```

I have got the following buffers

```auto
#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

```auto
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

```auto
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;
	  if(!tcpbuf)
	  {
		  uartBuffer = uartPpmBuffer;
		  process_func = data_process_ttcp;
		  find_func = find_circ_end_lsn;
	  }
	  else
	  {
		  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;
  }

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

  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;
      process_func(&mNode);
      startPosition = endPosition;
      startFind = endPosition;
      continue;
    }
    break;
  }
  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

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [September 23, 2021, 8:43am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-when-using-queues/13524/2 "2021-09-23T08:43:37Z")

</div>

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 ?

> [@addi](#):
>
> `#define TCP_MSS 4096`

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

---

<div class="post-metadata">

**Author:** ![addi](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@addi](https://forums.freertos.org/u/addi)\
**Post date:** [September 23, 2021, 11:01pm UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-when-using-queues/13524/3 "2021-09-23T23:01:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [September 24, 2021, 7:21am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-when-using-queues/13524/4 "2021-09-24T07:21:45Z")

</div>

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 !
