请有人解释一下为什么我在以下代码中的 dispatch_semaphore_wait 中收到错误 EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0):
-(void) initialize {
dispatch_queue_t queue = dispatch_queue_create("My queue", NULL);
dispatch_semaphore_t sem = dispatch_semaphore_create(1);
self.queue = queue;
self.sem = sem;
self.myarray = [[NSMutableArray alloc]init];
[self.myarray addObject: [[MyObject alloc] init]];
}
-(MyObject *) method1 {
//do something
dispatch_semaphore_wait(self.sem, DISPATCH_TIME_FOREVER);
MyObject *obj = [self.myarray objectAtIndex:0];
dispatch_barrier_sync(self.queue, ^{
[self.myarray removeObjectAtIndex:0];
});
return obj;
}
-(void) method2:(MyObject *)object {
//do something
dispatch_barrier_async(self.queue, ^{
[self.myarray addObject:object];
dispatch_semaphore_signal(self.sem);
});
}
我发现了类似的问题为什么这段代码会导致“EXC_BAD_INSTRUCTION”?,但在我的情况下,我使用的是 ARC,并且我没有明确写出 nowhere dispatch_release(sem);