# Using a one-shot software timer that cleans up after itself from within it's own callback

**URL:** <https://forums.freertos.org/t/using-a-one-shot-software-timer-that-cleans-up-after-itself-from-within-its-own-callback/2904>\
**Category:** Kernel\
**Created:** [July 30, 2014, 2:47pm UTC](https://forums.freertos.org/t/using-a-one-shot-software-timer-that-cleans-up-after-itself-from-within-its-own-callback/2904 "2014-07-30T14:47:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 30, 2014, 2:47pm UTC](https://forums.freertos.org/t/using-a-one-shot-software-timer-that-cleans-up-after-itself-from-within-its-own-callback/2904/1 "2014-07-30T14:47:51Z")

</div>

**[nurichard](https://sourceforge.net/u/nurichard/)** wrote on [Wednesday, July 30, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/800b4385/#2168):

Hello,

I would like to be able to occasionally create a one-shot timer using the standard API, but I’m not sure about the safety of deleting the timer from within it’s own callback. A pseudocode example should hopefully illustrate this below (error checking etc ommitted for clarity):

void foo(TimerHandle\_t xTimer)  
{  
xTimerDelete(xTimer)  
// Do something useful  
…  
}

void bar(void)  
{  
TimerHandle\_t tim;  
tim = xTimerCreate(NULL,100, false, NULL, foo);  
xTimerStart(tim, 0);  
}

Please can anyone tell me if they know this to be safe/unsafe.

Thank you,  
Richard

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 30, 2014, 3:36pm UTC](https://forums.freertos.org/t/using-a-one-shot-software-timer-that-cleans-up-after-itself-from-within-its-own-callback/2904/2 "2014-07-30T15:36:40Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, July 30, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/800b4385/#942a):

That should be ok. The timer callback function runs in the timer task, and deleting the the timer does not delete the timer task, and the code for the callback function still exists. Don’t use the xTimer parameter after it has been deleted though.

If you use the timer periodically do you need to delete it at all?
