AVCaptureSession – Stop Running – take a long long time

I use ZXing for an app, this is mainly the same code than the ZXing original code except that I allow to scan several time in a row (ie., the ZXingWidgetController is not necesseraly dismissed as soon as something is detected).

I experience a long long freeze (sometimes it never ends) when I press the dismiss button that call

  • zxing in xcode 4.5 and ios 6
  • Create png image from QRCode for iPhone
  • ZXing Library: Errors in iOS: private field 'cached_y_' is not used
  • Managing views while integrating zxing for iPhone
  • Alternative to zxing QR reader library for Java/Android?
  • Undefined symbols for architecture armv7 when using ZXing library in XCode 4.5
  • - (void)cancelled {
      //  if (!self.isStatusBarHidden) {
      //      [[UIApplication sharedApplication] setStatusBarHidden:NO];
      //  }
        [self stopCapture];
        wasCancelled = YES;
        if (delegate != nil) {
            [delegate zxingControllerDidCancel:self];


    - (void)stopCapture {
        decoding = NO;
    #if HAS_AVFF
        if([captureSession isRunning])[captureSession stopRunning];
        AVCaptureInput* input = [captureSession.inputs objectAtIndex:0];
        [captureSession removeInput:input];
        AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[captureSession.outputs objectAtIndex:0];
        [captureSession removeOutput:output];
        [self.prevLayer removeFromSuperlayer];
         // heebee jeebees here ... is iOS still writing into the layer?
         if (self.prevLayer) {
         layer.session = nil;
         AVCaptureVideoPreviewLayer* layer = prevLayer;
         [self.prevLayer retain];
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 12000000000), dispatch_get_main_queue(), ^{
         [layer release];
        self.prevLayer = nil;
        self.captureSession = nil;

    (please notice that the dismissModalViewController that remove the view is within the delegate method)

    I experience the freeze only while dismissing only if I made several scans in a row, and only with an iPhone 4 (no freeze with a 4S)

    Any idea ?



    Solutions Collect From Internet About “AVCaptureSession – Stop Running – take a long long time”

    According to the AV Cam View Controller Example calling startRunning or stopRunning does not return until the session completes the requested operation. Since you are sending these messages to the session on the main thread, it freezes all the UI until the requested operation completes. What I would recommend is that you wrap your calls in an Asynchronous dispatch so that the view does not lock-up.

    - (void)cancelled 
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [self stopCapture];
       //You might want to think about putting the following in another method
       //and calling it when the stop capture method finishes
       wasCancelled = YES;
       if (delegate != nil) {
            [delegate zxingControllerDidCancel:self];