2 years ago

#47746

test-img

Dhruvil Patel

How to properly deallocate semaphore?

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 ?

swift

grand-central-dispatch

semaphore

dispatchsemaphore

0 Answers

Your Answer

Accepted video resources