Task within a task – causing crash

Hi, I have a fairly straightforward bit of code as such. The task time for this function in the main loop is set for 50ms. static void prvReadCOMBufferTaskFunction( void *pvParameters )
{
    portTickType xLastCheckTime;
    xLastCheckTime = xTaskGetTickCount();
  
for(;;)
{
              vTaskDelayUntil( &xLastCheckTime, check_COM_SIGNAL_RATE );               //check to see if start sequence received (startCycle). If started,
              //initiate a task that will validate time taken. should never be
              //more than 10-12 seconds from start.
             if(startCycle )
             {
                      //launch task to check in 10 seconds to see if completeFlag is set
                      pdPass = xTaskCreate( prvCommCheckTaskFunction, ( signed char * ) “CommCheck”,  configMINIMAL_STACK_SIZE, NULL, 2,(xTaskHandle)&CommCheckTask );
            sprintf(msgBuf, “pdPass = %i”, pdPass); _DBG_(msgBuf);
} .      if(completeFlag)
        {
            //found end of sequence; Ok to delete the comm check task.
            if(CommCheckTask) vTaskDelete( (xTaskHandle) CommCheckTask );
             //then process data
            processData();
         }
} Here’s the function callback for the created function: it’s timer is set to 10 seconds static void prvCommCheckTaskFunction( void *pvParameters )
{
    portTickType xLastCheckTime;
 
// Initialize xLast***Times prior to the first call to vTaskDelayUntil()
xLastCheckTime = xTaskGetTickCount(); for(;;)
{
        vTaskDelayUntil( &xLastCheckTime, checkComFlag_RATE );
        _DBG_(”If you can read this, an incomplet rx buffer was landed.”);
        if( CommCheckTask ) vTaskDelete( (xTaskHandle) CommCheckTask );
    } } Here’s the problem.
I can run it once and see that it deletes the task.
If I try it a second time – it crashes the program, causing WDT to kick. Anything obviously wrong that I can change? Thanks.

Task within a task – causing crash

Sound like you should be using a software timer instead. Please see:

Task within a task – causing crash

One question, why are you casting CommCheckTask to a xTaskHandle, it should be one already (and casting &CommCheckTask to one is incorrect). Also, you need to take some care in deleting a task, as doing so stops it right were it is with NO cleanup, so any resources it creates are not released. In your case if it is in the vTaskDelayUntil call you should be ok, but if that ends and you are starting the _DBG call, that might cause problems.

Task within a task – causing crash

Hi, thanks for the help.
Not sure how that cast got in there? I thought most of my code was from FreeRTOS examples, but none of the other tasks have that cast. Thanks for pointing that out. When you refer to cleanup, are you referring to FreeRTOS, or my code? If my code, yes, I have other stuff, just a few variables to set, which I would do before the delete. At this point I was just getting it to work repetitiously. The function “prvReadCOMBufferTaskFunction”  has been working with no issues at all before the above. Any other suggestions – or is it just the task handle you think is causing this?

Task within a task – causing crash

The casts can only cause trouble if CommCheckTask wasn’t defined as a xTaskHandle. For cleanup, I was referring to things like allocating memory in the task, or acquiring semaphores. One other thought is the code you posted doesn’t check that the xTaskCreate call succeeds. It is possible that you run out of memory. A few quick questions, which heap version are you using? Also, is the idle task able to get time to run? In you application it MUST be given time to run, as vTaskDelete does not put the memory from the task back into the heap, but postpones that operation to the idle task. If you have a task, above priority 0, that doesn’t block, you will not get your memory freed.

Task within a task – causing crash

Sorry, in the loop below it is actually this:  if(completeFlag && !pdPass) (have pdPass declared as well) I’ll add in the idle task and see if that changes anything. Thanks again for pointing that out. Gr

Task within a task – causing crash

friedl – maybe I could use one. I’ll read up on that and check it out. Thanks!