Delete the timer from call back function

saadsaeed wrote on Tuesday, September 18, 2018:

Hello,
Can i somehow get the timer handler from within the callback function and delete it?

richarddamon wrote on Tuesday, September 18, 2018:

The callback function is given a void* pointer, you can point that to the timer, or to a structure with a pointer to the timer.

richarddamon wrote on Tuesday, September 18, 2018:

The callback function is given a void* pointer, you can point that to the timer, or to a structure with a pointer to the timer.

saadsaeed wrote on Tuesday, September 18, 2018:

Can you please give me an example?

saadsaeed wrote on Tuesday, September 18, 2018:

Thsi is what I am doing.

static void vTimerCallBackinitial( TimerHandle_t xTimer )
{
timer_info=xTimerDelete(xTimer,0);//Delete the timer
}
void doip_initial_activity_timer(int id)
{

		// Save the socket id in the currently available instance
			TimerHandle_t  one_shot_timer;
		BaseType_t xTimer1Started;
		one_shot_timer=xTimerCreate(
				/* Text name for the software timer - not used by FreeRTOS. */
				"Initial",
				/* The software timer's period in ticks. */
			INITIAL_ACTIVITY_TIMER_PERIOD,
				/* Setting uxAutoRealod to pdFALSE creates a one-shot software timer. */
				pdFALSE,
				/* This example does not use the timer id. */
				(void*)id,
				/* The callback function to be used by the software timer being created. */
				vTimerCallBackinitial);
		if(one_shot_timer != NULL)
		{
			xTimer1Started=xTimerStart(one_shot_timer, 0 );
			//Start the Multi shot timer
			if( ( xTimer1Started == pdPASS ))
			{
			print("Timer created");
			}

		}
	
	else
	{
		print("Timer not created\n");


	}
}

saadsaeed wrote on Tuesday, September 18, 2018:

Above, I have posted mu code. Is it okay?

rtel wrote on Tuesday, September 18, 2018:

I don’t think this is a valid thing to do. The callback function is called using the following code:

pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );

where pxTimer is pointing to the same structure as the handle passed into the callback function. If the structure is deleted in the callback function then it is the structure that is being de-referenced that is being deleted. As the code stands today it should be ok as the timer callback just returns and the structure does not get referenced again after that - but that doesn’t mean it will be ok in future versions.