Reputation: 1416
I have the following C++ wrapper for the FreeRTOS task:
class Threat {
public:
Threat(const char* name, uint32_t stackSize, uint8_t priority):
m_Name(name), m_StackSize(stackSize), m_Priority(priority){
xTaskCreate(&task_function, "MYTASK", m_StackSize, this, m_Priority, &m_TaskHandle);
}
static void task_function(void *p){
int i = 0;
Threat* self = static_cast<Threat*>(p);
while(1){
i++;
vTaskDelay(1000);
}
}
private:
const char* m_Name;
const uint32_t m_StackSize;
const uint8_t m_Priority;
TaskHandle_t m_TaskHandle;
};
And in my main:
int main(void){
Threat task1("task1", 128, tskIDLE_PRIORITY);
schedulerStart();
}
When I run this, FreeRTOS correctly calls the task_function, BUT if I look into the self variable inside the function, the data has been changed. The pointer is correct, but the data for the task1 has been changed (and it is not the cast that goes wrong - I've checked the memory).
Anybody have a seen this before?.. I use CoIDE and GCC embedded.
EDIT: This is how the data looks in main:
And in the task function:
Upvotes: 0
Views: 871
Reputation: 48
Threat task1(...);
is an automatic variable allocated on the stack, and since it is not explicitly referenced later in the scope, this means that it's memory is reclaimed.
You could declare task1 in global scope, add 'static' to it's declaration, or cheat by referencing task1 in the bottom of main(), e.g.
printf("%s\n",task1.m_name);
(That would require that you do not declare m_name as private)
Upvotes: 3