pthread_mutex_unlock is returning EINVAL randomly

The mutex itself is stored in a shared list, so it should be valid. However, at random times, crash happens due to sigabrt with the trace below:


[ 4] 0x00007fff86b513d4 libsystem_pthread.dylib`__pthread_abort_reason + 179

[ 5] 0x00007fff86b4ddf0 libsystem_pthread.dylib`_pthread_mutex_unlock_drop + 160


Looking at the code it's not obvious if something can be causing the mutex value to be invalid, is there a good way to debug this?

Replies

> The mutex itself is stored in a shared list, so it should be valid.


What does this mean? What's a "shared list" in this context? Why does that imply the mutex should be valid?


How was the mutex created? What attributes? Have you tried pthread_mutexattr_settype() with PTHREAD_MUTEX_ERRORCHECK (if you're not already using PTHREAD_MUTEX_RECURSIVE)?


Is the unlock call happening on the same thread which locked the mutex?


Is there any chance you have a memory smashing bug that's overwritten parts of the mutex object? What's just before or after it in memory? Perhaps you're over/underflowing that?