Fix undefined behaviour caused by mutex unlock() in destructor