fixed memory leaks  (obtaining references to semaphores again and again)