# Problem with Static Task Creation - TrueStudio, freeRTOS, "Invalid selected thread"

**URL:** <https://forums.freertos.org/t/problem-with-static-task-creation-truestudio-freertos-invalid-selected-thread/7422>\
**Category:** Kernel\
**Created:** [September 17, 2018, 9:28am UTC](https://forums.freertos.org/t/problem-with-static-task-creation-truestudio-freertos-invalid-selected-thread/7422 "2018-09-17T09:28:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 17, 2018, 9:28am UTC](https://forums.freertos.org/t/problem-with-static-task-creation-truestudio-freertos-invalid-selected-thread/7422/1 "2018-09-17T09:28:40Z")

</div>

**[alho](https://sourceforge.net/u/alho/)** wrote on [Monday, September 17, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/41feb7b7/#db20):

Hi,

I’m troubleshooting a problem with task creation in freeRTOS, Atollic TrueStudio, STM32.

In the first call of _xTaskCreateStatic()_ the code runs into an error: “Invalid selected thread”. When debugging I can see that the _pxNewTCB_ gets initialized correctly in _prvInitialiseNewTask( )_. But in _prvAddNewTaskToReadyList( )_ the code breaks at the assignment of _pxNewTCB_ to _pxCurrentTCB_:

```auto
static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB )
{
	/* Ensure interrupts don't access the task lists while the lists are being
	updated. */
	taskENTER_CRITICAL();
	{
		uxCurrentNumberOfTasks++;
		if( pxCurrentTCB == NULL )
		{
			/* There are no other tasks, or all the other tasks are in
			the suspended state - make this the current task. */
			pxCurrentTCB = pxNewTCB;
            ...

```

I couldn’t find something related to this problem in the freeRTOS or STM/Atollic forum but something that looks similar in the openstm32 forum [http://www.openstm32.org/forumthread2824](http://www.openstm32.org/forumthread2824) …but it didn’t help me.

I double checked the task settings, priority, stack size,… but I don’t find something that looks wrong.  
If I deactivate the first taks creation (call of the _xTaskCreateStatic()_ ) it gets stuck at the second (or now first) call of _xTaskCreateStatic()_.

Any ideas what might cause the problem and how to solve it?

If I shall post more stuff, please let me know!  
I’m using TrueStudio V9, freeRTOS V9, basic code generated with CubeMX(V4.25.1), Debugger j-link Ultra+.

Regards,  
Alex

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 18, 2018, 7:09am UTC](https://forums.freertos.org/t/problem-with-static-task-creation-truestudio-freertos-invalid-selected-thread/7422/2 "2018-09-18T07:09:39Z")

</div>

**[alho](https://sourceforge.net/u/alho/)** wrote on [Tuesday, September 18, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/41feb7b7/#c207):

Hi,

an addition; I was experimenting with the Debug Config.

When I deactivate Thread-aware RTOS support (No RTOS), debugging the code seems to work. I can still debug RTOS for SEGGER SysView. The code runs good, didn’t find something unexpected yet. Anyway I can’t tell why this could be a problem, because I got the “extra” expression…

```auto
thread 2
continue

```

…in my Startup Script. But if this is the best solution, I will do it like that.

Next experiment:  
When I deactivate Thread-aware RTOS support AND Trace System (I use SWV), the code runs into assertion. It then seems to mess up with SystemView, because when I deactivate SystemView (not calling _SEGGERSYSVIEWConf()_ ), the code is running without problems.

Well, at least I found a workaround to be able to keep on working. But still I have the feeling that there must be something horrible wrong in the project…maybe somebody has an idea with this new information?

Regards,  
Alex

---

<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:** [September 18, 2018, 7:52am UTC](https://forums.freertos.org/t/problem-with-static-task-creation-truestudio-freertos-invalid-selected-thread/7422/3 "2018-09-18T07:52:48Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, September 18, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/41feb7b7/#93f3):

For some reason my reply to your first post, some ten hours ago, never showed up here. It was:

I’m afraid your description isn’t really clear enough for me to determine what the issue you are reporting is.

> In the first call of /xTaskCreateStatic()/ the code runs into an error: “Invalid selected thread”.

Where is this error, by which I mean how did you determine that was the error?

> When debugging I can see that the /pxNewTCB/ gets initialized correctly in /prvInitialiseNewTask( )/. But in /prvAddNewTaskToReadyList( )/ the code breaks at the assignment of /pxNewTCB/ to /pxCurrentTCB/:

What is meant by the code ‘breaking’? Does it crash, and if so, how does that crash occur? Is it related to the “Invalid selected thread” error from above?

> I double checked the task settings, priority, stack size,… but I don’t find something that looks wrong.  
> If I deactivate the first taks creation (call of the /xTaskCreateStatic()/ ) it gets stuck at the second (or now first) call of /xTaskCreateStatic()/

Do you have configASSERT() defined so it checks the size of the static task structure against the non static task structure? If you have something like configUSE\_LIST\_DATA\_INTEGRITY\_CHECK\_BYTES defined then the structures could be different sizes, which would cause a problem.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 19, 2018, 2:10pm UTC](https://forums.freertos.org/t/problem-with-static-task-creation-truestudio-freertos-invalid-selected-thread/7422/4 "2018-09-19T14:10:32Z")

</div>

**[alho](https://sourceforge.net/u/alho/)** wrote on [Wednesday, September 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/41feb7b7/#197e):

Hi Richard,

thanks for the reply. I’ll try to answer your questions.  
The problem, however, is that the error is gone. I can’t reproduce it right now. I jumped back in my code history when it occured but I can’t trigger it now…must be evil black magic.

The error was outputted on the debugging console. The code crashed and I was able to isolate it to the assignment mentioned above (pxCurrentTCB = pxNewTCB). The scheduler didn’t start at that point yet, it was all during initialization.

At the moment it’s hard to tell you more as I cannot reproduce it. configASSERT() is defined, configUSE\_LIST\_DATA\_INTEGRITY\_CHECK\_BYTES is not defined (gets defined to 0 in projdef.h by default).

I will try to reproduce the error and make more and detailed tests if it occurs.
