How to properly deallocate semaphore?

51 Views Asked by At

Steps to reproduce crash:
If semaphore.wait() is called and then deinitialize the view controller that owns it before calling semaphore.signal, app crashes.

Getting below error on dinit of the class:

EXC_BAD_INSTRUCTION with the message "BUG IN CLIENT OF LIBDISPATCH: Semaphore object deallocated while in use"

Used:

let semaphore = DispatchSemaphore(value: 1)

But weirdly adding below mentioned code under deinit fixes the crash issue, don't know why.

 while (semaphore.signal() != 0) {}

So is there any proper fix ?

0

There are 0 best solutions below