# Program goes into an infinite loop upon new task creation

**URL:** <https://forums.freertos.org/t/program-goes-into-an-infinite-loop-upon-new-task-creation/12941>\
**Category:** Kernel\
**Created:** [June 27, 2021, 9:18pm UTC](https://forums.freertos.org/t/program-goes-into-an-infinite-loop-upon-new-task-creation/12941 "2021-06-27T21:18:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 27, 2021, 9:18pm UTC](https://forums.freertos.org/t/program-goes-into-an-infinite-loop-upon-new-task-creation/12941/1 "2021-06-27T21:18:58Z")

</div>

I’m trying to create a new task in a sensor class constructor but as soon as the task scheduler runs, it goes into a forever loop

```auto
  /* Loop forever */
exit_loop:
  b exit_loop
#endif

```

when it’s not created, the program seems to run fine. What is it about this new task that might possibly be causing the program into an infinite loop?

The callstack goes like:

```auto
Unknown function at 0xFFFFFFFC
void prvCheckForValidListAndQueue()
exit()
```

---

<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 28, 2021, 12:02am UTC](https://forums.freertos.org/t/program-goes-into-an-infinite-loop-upon-new-task-creation/12941/2 "2021-06-28T00:02:48Z")

</div>

I am ASSUMING exit\_loop gets called if the program attempts to exit, either by calling exit or running off the end of the program. In the latter case it may be due to running out of heap memory. When you call vTaskStartScheduler() the kernel creates the idle task, and optionally the timer task. Each task requires a stack. The only time vTaskStartScheduler() should ever return is if there is insufficient heap memory remaining to allocate the stack of one or both of those tasks.

See the description of the Malloc Failed Hook function here: [https://www.freertos.org/a00016.html](https://www.freertos.org/a00016.html)

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 28, 2021, 12:41am UTC](https://forums.freertos.org/t/program-goes-into-an-infinite-loop-upon-new-task-creation/12941/3 "2021-06-28T00:41:00Z")

</div>

Spot on. Just added `vApplicationMallocFailedHook` and it gets called.

So I added some prints to determine how much heap is being used by each task (3 so far each allocating 256 bytes which I randomly picked) via `xPortGetFreeHeapSize()` and I notice that before the allocation of the first task, heap size = 4000 bytes, and after the last task, the only remaining heap size is 600 bytes. And I guess that’s mostly used up by the idle task and the timer task after calling the scheduler?

How do I know if 256 bytes per task is sufficient or too much for my needs?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [June 28, 2021, 1:02am UTC](https://forums.freertos.org/t/program-goes-into-an-infinite-loop-upon-new-task-creation/12941/4 "2021-06-28T01:02:23Z")

</div>

Best way is to look at the code that gets generated and figure out how much stack is really needed. One thing to watch out for is that stack size is specified in WORDS not BYTES.
