title: Why I get error EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) when I use GCD
body: <p>Could somebody, please, explain me why I get error EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) in dispatch_semaphore_wait in the following code:</p>
  
  <pre><code>-(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);
    });
  }
  </code></pre>
  
  <p>I found similar question <a href="https://stackoverflow.com/questions/8287621/why-does-this-code-cause-exc-bad-instruction">Why does this code cause &quot;EXC_BAD_INSTRUCTION&quot;?</a>, but in my case I am using ARC and I do not write explicitly nowhere dispatch_release(sem);</p>