title: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose
body: <p>I am getting EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose but don't really know how to track down the root cause of this.  My code makes use of dispatch_async, dispatch_group_enter and so on.</p>
  
  <p>UPDATE:
  The cause of the crash is due to the fact that the webserviceCall (see code below) never calls onCompletion and when the code is run again, I got the error EXC_BAD_INSTRUCTION.  I verified this is indeed the case, but not sure why or how to prevent this.</p>
  
  <p><img src="https://i.stack.imgur.com/Tsud3.jpg" alt="enter image description here"></p>
  
  <p>Code:</p>
  
  <pre><code>dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
  dispatch_group_t group = dispatch_group_create();
  
       for (...) {
          if (...) {
              dispatch_group_enter(group);
              dispatch_async(queue, ^{
  
                 [self webserviceCall:url onCompletion:^{
                       dispatch_group_leave(group);
                 }];
              });
          }
      }
  
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)));
      dispatch_sync(queue, ^{
          // call completion handler passed in by caller
      });
  });
  </code></pre>