I have a binary semaphore for sharing a mem buffer between a few tasks.
A simple example is the best way for me to explain the problem:
taskA → takes the binary semaphore
taskB → calls xSemaphoreTake() and waits for the semaphore to be released
taskA → shared mem is no longer needed, so deletes the shared mem and the semaphore along with it
taskB → xSemaphoreTake() causes a hard-fault.
You don’t delete a resource that other tasks might be using. If the shared memory is no longer needed, why is Task B taking the semaphore to access it?
It seems the error here is Task A just deciding that the shared memory is no longer needed while Task B was still thinking it was available.
Task A can’t properly come to the conclusion that it is no longer needed, until everyone that has known about it somehow signals that it is done with it and won’t use it any more.
It’s a graphics buffer for an item on a display.
taskB (renderer) is rendering the item.
taskA (user-interaction/control) is removing it because it no longer needs to exist.
the fault occurrs because you delete a semaphore that another task waits on. Do not do that.
Edit: One option would be to re-signal the semaphore after setting some kind of shared indication that allows the waiting task to clean up, ie delete the semaphore.
Just because the user-interface decided it didn’t need to exist, doesn’t mean it can be removed immediately. If it started a “background process” on the item, it needs to wait for the process to finish, or request the abort and wait for confirmation before destroying it.