jon_newcomb wrote on Thursday, February 14, 2008:
I had a situation where trimming task priorities resulted in a NULL pointer reference, which scared me slightly.
Also the code to start the new task was a function call used by lots of tasks of differing priorities.
The way I got round this was to use pvPortMalloc() and let the newly generated task free the memory using vPortFree().
WHAT DOES THE CODE DO?
Its a one shot timer. It fires off a new tasks that waits for the specified period of time, posts a message to a queue and then destroys itself.
I allocate memory for a ‘param_*’ struct and stuff it with local variables from calling task
A new task is started and receives a pointer to the struct. The struct is cast to access data. Once used it can be freed.
It did strike me as a safe way, but rather long winded.
Has anyone else solved this problem in a less round about way?
typedef struct prt_param_oneshottimer_t
TaskStart_OneShotTimer (portTickType timerperiod, uint32_t event, uint32_t data)
int retval = PASS;
// Allocate storage for the timer variables. Pointer to this is
// passed to the task and used by the task created
param_oneshottimer = ( param_oneshottimer_t * )
pvPortMalloc( sizeof( param_oneshottimer_t ) );
// Assign passed arguments to the task
param_oneshottimer->timerperiod = timerperiod;
param_oneshottimer->event = event;
param_oneshottimer->data = data;
// Pass arguments to the task that starts the timer
(Task_OneShotTimer, "OneShotTimer", TASKSTACK_TIMER,
(void *)param_oneshottimer, (tskIDLE_PRIORITY + 3),
(xTaskHandle *) NULL) != PASS)
retval = FAIL;
portTASK_FUNCTION (Task_OneShotTimer, pvParameters)
// Cast data passed to task
prt_param_oneshottimer = (param_oneshottimer_t *) pvParameters;
// Wait for the specified period of time
// We have waited the required amount of time. Post the required
// message to the queue
// Deallocate memory used
vPortFree( (void *)prt_param_oneshottimer );
// Now delete the task