Passing parameter from main to various tasks

nasher128 wrote on Saturday, September 21, 2019:

Hi all.

I currently have a task running which I’m passing a C++ object that is created within main’s stack but it seems I’m getting some corruption when I’m looking at the data. I don’t get the problem when I declare the object being passed in as static (which I’d expect). The IPC object is being passed by reference and as the main routine never exits due to the vTaskStartScheduler(), therefore the “ipc_handle” object is never destroyed? As stated, everything works when I declare IPC as static.

int main(void)
{

	IPC ipc_handle;

	UserMonitor task("UserMonitor",1, ipc_handle);
    
    vTaskStartScheduler();

	/*
    }

aggarg-aws wrote on Saturday, September 21, 2019:

Which hardware platform are you running on? On Cortex-M platforms, we recover the main stack when the scheduler is started and as a resulted anything created on the main stack is destroyed. The rationale of doing so is that the complete main stack is available to Interrupt Service Routines.

So what you are observing is the expected behavior and as you mentioned, you can fix it by declaring the variable static in which it won’t be created on main stack.

Thanks.

nasher128 wrote on Saturday, September 21, 2019:

Arghhhh it’s been driving me crazy for the pass few hours. I’m using a STM32F7 chip (cortex m7). Do you happen to have any resources about that you’ve stated, as I want to read into this more, just in-case I come across anything else which could catch me out again.

Thanks

aggarg-aws wrote on Saturday, September 21, 2019:

You can look at the detailed discussion here: https://www.freertos.org/FreeRTOS_Support_Forum_Archive/January_2015/freertos_Main_stack_pointer_reset_when_starting_the_scheduler_e5a776c1j.html

Bottom point is, do not create anything on the main stack as that is inaccessible after the scheduler is started.

Thanks.