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.