Cococa初学者使用锁的坑(Exec_bad_Instruction)

来源:互联网 发布:网络教育本科毕业论文 编辑:程序博客网 时间:2024/05/29 16:21

在开发过程中,为了访问互斥资源或者更细粒度的串行控制, 一般都会使用锁机制。使用最多的应该是

@synchronized, Semaphore, ConditionLock


@synchronized使用起来比较方便, 但是如果访问互斥资源的频率较高,这样做的话效率就比较低了


semaphore我相对使用较多, 但是过程中也遇到不少的坑, 最近就在使用不当上遭遇了crash

系统报(sigill)错误, 指令无法执行


查看crash日志。 发现几行比较重要的标记:


__dispatch_semaphore_dispose:000040a0            b590        push    {r4, r7, lr}000040a2            4604        mov     r4, r0000040a4            af01        add     r7, sp, #4000040a6        e9d40108        ldrd    r0, r1, [r4, #32]000040aa            4288        cmp     r0, r1000040ac            da00        bge.n   0x40b0000040ae            defe        trap...
错误从40AE开始, 在执行40AA的时候报错, cmp r0 r1的时候报错, 提示:
Semaphore/group object deallocated while in use
在libDispatch中可以定位到这句话:
_dispatch_semaphore_dispose(dispatch_semaphore_t dsema){if (dsema->dsema_value < dsema->dsema_orig) {DISPATCH_CLIENT_CRASH("Semaphore/group object deallocated while in use");}

在semaphore调用dispose的时候不能让currentValue < originValue, 如果出现就会导致指令执行错误, 系统发出sigill信号中断程序执行
查看代码中使用信号量的地方:
定位到了分量上传的API,  在分量上传失败的时候, 我会发出终止信号, 阻断继续分量的upload, 但是在终止之前, 我发送了一次wait事件,
导致信号量在释放的时候, currentValue为-1, 最终程序强制终止。

0 0
原创粉丝点击