# xSemaphoreTake return value

**URL:** <https://forums.freertos.org/t/xsemaphoretake-return-value/21534>\
**Category:** Kernel\
**Created:** [September 26, 2024, 1:54pm UTC](https://forums.freertos.org/t/xsemaphoretake-return-value/21534 "2024-09-26T13:54:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmrivas70](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@jmrivas70](https://forums.freertos.org/u/jmrivas70)\
**Post date:** [September 26, 2024, 1:54pm UTC](https://forums.freertos.org/t/xsemaphoretake-return-value/21534/1 "2024-09-26T13:54:52Z")

</div>

Hello

I would like to know if there is a way to tell the difference between a TIME OUT and other ERROR when taking a semaphore.

For instance, if you try to take a semaphore which was previously deleted.

Thanks in advance,  
Jose

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [September 26, 2024, 2:46pm UTC](https://forums.freertos.org/t/xsemaphoretake-return-value/21534/2 "2024-09-26T14:46:43Z")

</div>

Hi @jmrivas70  
`xSemaphoreTake` returns pdFALSE if `xTicksToWait` expired without the semaphore becoming available (TIMEOUT)  
In the other scenario you mention, where the API is called with a previously deleted semaphore, then the memory location pointed by the semaphore should be NULL.  
So , in the API `xQueueSemaphoreTake` ( configASSERT should be defined) , the code should assert [here](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/queue.c#L1666)

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 26, 2024, 3:43pm UTC](https://forums.freertos.org/t/xsemaphoretake-return-value/21534/3 "2024-09-26T15:43:30Z")

</div>

> [@jmrivas70](#):
>
> For instance, if you try to take a semaphore which was previously deleted.

Calling an API with a semaphore handle that has been deleted is an application bug and must not be done. The application must ensure that no other task is using the object before deleting it.

---

<div class="post-metadata">

**Author:** ![Xavier](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@Xavier](https://forums.freertos.org/u/Xavier)\
**Post date:** [October 2, 2024, 4:10am UTC](https://forums.freertos.org/t/xsemaphoretake-return-value/21534/4 "2024-10-02T04:10:56Z")

</div>

**Accessing a previously deleted object is a critical programming error.** This can lead to unpredictable behavior, as your application may sometimes fail and sometimes not. It’s essential to handle memory management carefully to avoid such issues.

**Immediately after deleting an object (including semaphores), set the corresponding pointer to `NULL`.** This way, if you accidentally dereference the pointer later, your application will fail predictably, making it easier to identify and fix the problem.

**Depending on your use case, consider using statically allocated objects.** While dynamically allocated objects offer flexibility, static allocation can be more appropriate in certain scenarios, especially when you need to ensure that objects remain in memory for the entire lifetime of your application.
