# pvPortMalloc returning NULL when there is no doubt I have enough memory (PSOC5LP)

**URL:** <https://forums.freertos.org/t/pvportmalloc-returning-null-when-there-is-no-doubt-i-have-enough-memory-psoc5lp/10058>\
**Category:** Kernel\
**Created:** [June 17, 2020, 10:19pm UTC](https://forums.freertos.org/t/pvportmalloc-returning-null-when-there-is-no-doubt-i-have-enough-memory-psoc5lp/10058 "2020-06-17T22:19:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunerm99](https://avatars.discourse-cdn.com/v4/letter/b/f4b2a3/32.png) [@brunerm99](https://forums.freertos.org/u/brunerm99)\
**Post date:** [June 17, 2020, 10:19pm UTC](https://forums.freertos.org/t/pvportmalloc-returning-null-when-there-is-no-doubt-i-have-enough-memory-psoc5lp/10058/1 "2020-06-17T22:19:25Z")

</div>

Hi,

I am very new to FreeRTOS and I keep getting an error on when I try to start the task scheduler (vTaskStartScheduler). pvPortMalloc is returning NULL which causes vApplicationMallocFailedHook to be called but I do not have one defined. I disabled this in the config but when it is disabled it compiles and programs but gets stuck in an infinite loop because I am out of heap space (The infinite loop is in Cm3Start.c).

I have tried allocating more heap space in the config (even though the only task being run turns an LED on so it shouldn’t need anywhere near 64k).

My main.c

```auto
<------------------------------------------------------------>
#include "project.h"
#include "FreeRTOS.h"
#include "timers.h"
//#include "led_control.h"

void led_stuff( void *pvParams );

int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    
    TaskHandle_t xHandle = NULL;

    if (xTaskCreate(led_stuff, "led_flicker", ( uint16 ) 64, NULL, 1, &xHandle) == pdPASS) {
        vTaskStartScheduler();
        vTaskDelete(xHandle);
    }
    

    for(;;)
    {
    }
}

void led_stuff( void *pvParams ){
    ( void ) pvParams;
    
    for(;;){
        LED_Write(1u);
        CyDelay(200);
        LED_Write(0u);
        CyDelay(200);    
    }
}
<------------------------------------------------------>

```

Thank you in advance for help and sorry for bad formatting.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 17, 2020, 11:23pm UTC](https://forums.freertos.org/t/pvportmalloc-returning-null-when-there-is-no-doubt-i-have-enough-memory-psoc5lp/10058/2 "2020-06-17T23:23:05Z")

</div>

How are you increasing the heap size? If you are using anything other than [heap\_3.c](https://www.freertos.org/a00111.html) then the total heap size is set by configTOTAL\_HEAP\_SIZE and increasing heap anywhere else will just allocate memory to a heap that is not being used. If that is how you are increasing the heap size then perhaps your C startup code is not initialising variables correctly - try stepping into the calls to pvPortMalloc() in the debugger to see why it thinks it has run out of memory.

---

<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:** [June 18, 2020, 5:55am UTC](https://forums.freertos.org/t/pvportmalloc-returning-null-when-there-is-no-doubt-i-have-enough-memory-psoc5lp/10058/3 "2020-06-18T05:55:01Z")

</div>

Also it might be better to use `configMINIMAL_STACK_SIZE `or more for the stack size of your task instead of literal value `64` except you‘re sure that it‘s ok.
