xSemaphoreTake return value

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

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

1 Like

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.

2 Likes

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.

1 Like