Recursive Callback call crashing app after some it

2019-09-16 05:57发布

问题:

I'm making an iOS app that continuously takes two pictures and does post processing on them, then displays it in UI. The only way I was able to capture two pictures synchronously was by using recursion inside the completionHandler of captureStillImageAsynchronouslyFromConnection function in AVCaptureStillImageOutput. The app eventually crashes after taking around 30 photos, here is the crash log for it:

Date/Time:           2017-05-18 14:06:38.5714 -0400
Launch Time:         2017-05-18 14:06:22.1703 -0400
OS Version:          iPhone OS 10.3.1 (14E304)
Report Version:      104

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d
Triggered by Thread:  0

Filtered syslog:
None found

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x000000018c3b3260 semaphore_wait_trap + 8
1   libdispatch.dylib               0x000000018c2a15e8 _os_semaphore_wait + 24
2   libdispatch.dylib               0x000000018c2a0ca0 _dispatch_semaphore_wait_slow + 140
3   Camera-oxilight                 0x00000001000f5c14 0x1000f0000 + 23572
4   UIKit                           0x000000019353b0ec -[UIApplication sendAction:to:from:forEvent:] + 96
5   UIKit                           0x000000019353b06c -[UIControl sendAction:to:forEvent:] + 80
6   UIKit                           0x00000001935255e0 -[UIControl _sendActionsForEvents:withEvent:] + 440
7   UIKit                           0x0000000193542ce4 -[UIControl touchesBegan:withEvent:] + 312
8   UIKit                           0x000000019353a258 -[UIWindow _sendTouchesForEvent:] + 1948
9   UIKit                           0x0000000193535804 -[UIWindow sendEvent:] + 3192
10  UIKit                           0x0000000193506418 -[UIApplication sendEvent:] + 340
11  UIKit                           0x0000000193cfff64 __dispatchPreprocessedEventFromEventQueue + 2400
12  UIKit                           0x0000000193cfa6c0 __handleEventQueue + 4268
13  UIKit                           0x0000000193cfaaec __handleHIDEventFetcherDrain + 148
14  CoreFoundation                  0x000000018d385424 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
15  CoreFoundation                  0x000000018d384d94 __CFRunLoopDoSources0 + 540
16  CoreFoundation                  0x000000018d3829a0 __CFRunLoopRun + 744
17  CoreFoundation                  0x000000018d2b2d94 CFRunLoopRunSpecific + 424
18  GraphicsServices                0x000000018ed1c074 GSEventRunModal + 100
19  UIKit                           0x000000019356b130 UIApplicationMain + 208
20  Camera-oxilight                 0x00000001000f8618 0x1000f0000 + 34328
21  libdyld.dylib                   0x000000018c2c159c start + 4

Thread 1:
0   libsystem_kernel.dylib          0x000000018c3d1a88 __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x000000018c497274 _pthread_wqthread + 1260
2   libsystem_pthread.dylib         0x000000018c496d7c start_wqthread + 4

Thread 2:
0   libsystem_pthread.dylib         0x000000018c496d78 start_wqthread + 0

Thread 3:
0   libsystem_pthread.dylib         0x000000018c496d78 start_wqthread + 0

Thread 4 name:  com.apple.uikit.eventfetch-thread
Thread 4:
0   libsystem_kernel.dylib          0x000000018c3b3224 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x000000018c3b309c mach_msg + 72
2   CoreFoundation                  0x000000018d384e88 __CFRunLoopServiceMachPort + 192
3   CoreFoundation                  0x000000018d382adc __CFRunLoopRun + 1060
4   CoreFoundation                  0x000000018d2b2d94 CFRunLoopRunSpecific + 424
5   Foundation                      0x000000018ddccd64 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304
6   Foundation                      0x000000018ddedb34 -[NSRunLoop(NSRunLoop) runUntilDate:] + 96
7   UIKit                           0x0000000193ef55f8 -[UIEventFetcher threadMain] + 136
8   Foundation                      0x000000018deca2c8 __NSThread__start__ + 996
9   libsystem_pthread.dylib         0x000000018c49975c _pthread_body + 240
10  libsystem_pthread.dylib         0x000000018c49966c _pthread_body + 0
11  libsystem_pthread.dylib         0x000000018c496d84 thread_start + 4

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x000000000000000e   x1: 0x0000000000008433   x2: 0x0000000000000028   x3: 0x0000000000000030
    x4: 0x000000000000060f   x5: 0x0000000000000000   x6: 0x0000000000000000   x7: 0x00000000000000e0
    x8: 0x0000000000000000   x9: 0x0000000080001200  x10: 0xffffffffffffffff  x11: 0x0000000000000000
   x12: 0x00000001b5c9da4c  x13: 0x000000000000006a  x14: 0x0000000000000001  x15: 0x0000000000000881
   x16: 0xffffffffffffffdc  x17: 0x0000000000000000  x18: 0x0000000000000000  x19: 0x000000017008ff88
   x20: 0x000000017008ff50  x21: 0xffffffffffffffff  x22: 0x0000000111e02aa0  x23: 0x0000000111e02aa0
   x24: 0x000000017400ec30  x25: 0x0000000000000000  x26: 0x00000001940448d1  x27: 0x0000000000000001
   x28: 0x0000000174051c40   fp: 0x000000016fd0d7e0   lr: 0x000000018c2a15e8
    sp: 0x000000016fd0d7d0   pc: 0x000000018c3b3260 cpsr: 0x60000000

Binary Images:
0x1000f0000 - 0x10011ffff Camera-oxilight arm64  <e7b2ec7f7ae531669038395b0b628ed5> /var/containers/Bundle/Application/1FC949F0-FBD1-4502-A2CC-E9EE5009BC03/Camera-oxilight.app/Camera-oxilight
0x100194000 - 0x1001c7fff dyld arm64  <a63e8b89c75a3115b54b1f2f469f676a> /usr/lib/dyld
0x10021c000 - 0x1002bffff GPUImage arm64  <60950acd64553c05b59f66be8c6daee6> /var/containers/Bundle/Application/1FC949F0-FBD1-4502-A2CC-E9EE5009BC03/Camera-oxilight.app/Frameworks/GPUImage.framework/GPUImage
0x18bdb8000 - 0x18bdb9fff libSystem.B.dylib arm64  <6d9ab1f5df1b36d89fd5675936e3da5e> /usr/lib/libSystem.B.dylib
0x18bdba000 - 0x18be0ffff libc++.1.dylib arm64  <4d91c4d8858339c7ae2b3716d1f5e0fc> /usr/lib/libc++.1.dylib
0x18be10000 - 0x18be2cfff libc++abi.dylib arm64  <5615fb6378773e82a20d5d0727a6132e> /usr/lib/libc++abi.dylib
0x18be30000 - 0x18c20dfff libobjc.A.dylib arm64  <64c3c5a56c7a30c39ff4a3ec74426cf4> /usr/lib/libobjc.A.dylib
0x18c20e000 - 0x18c212fff libcache.dylib arm64  <f507d09bab2d343c9b9c53a05986909b> /usr/lib/system/libcache.dylib
0x18c213000 - 0x18c21efff libcommonCrypto.dylib arm64  <0bd3d4cb2d803c6caf1d09e54e8dc705> /usr/lib/system/libcommonCrypto.dylib
0x18c21f000 - 0x18c222fff libcompiler_rt.dylib arm64  <c2952c9143233a30bbad9ffd3535c47c> /usr/lib/system/libcompiler_rt.dylib
0x18c223000 - 0x18c22afff libcopyfile.dylib arm64  <ee8e1650db9b3a57b3e517677ef1da49> /usr/lib/system/libcopyfile.dylib
0x18c22b000 - 0x18c28cfff libcorecrypto.dylib arm64  <1662015f100e3fab8573f40889935a98> /usr/lib/system/libcorecrypto.dylib
0x18c28d000 - 0x18c2bcfff libdispatch.dylib arm64  <46e0cb2039333474ba7b47b131153bd5> /usr/lib/system/libdispatch.dylib
0x18c2bd000 - 0x18c2c1fff libdyld.dylib arm64  <649eb4fd79bf30869584b3ec86b6bcbc> /usr/lib/system/libdyld.dylib
0x18c2c2000 - 0x18c2c2fff liblaunch.dylib arm64  <985c8570c8603f8886372c8fe4843f08> /usr/lib/system/liblaunch.dylib
0x18c2c3000 - 0x18c2c8fff libmacho.dylib arm64  <3fdc8b3ebe27315aa71cadf73b0e0642> /usr/lib/system/libmacho.dylib
0x18c2c9000 - 0x18c2cafff libremovefile.dylib arm64  <7e353a2221703ccd99c8bb04a0bdc3dd> /usr/lib/system/libremovefile.dylib
0x18c2cb000 - 0x18c2e2fff libsystem_asl.dylib arm64  <2f456d47db4937c5aa3dee82ab2550ee> /usr/lib/system/libsystem_asl.dylib
0x18c2e3000 - 0x18c2e3fff libsystem_blocks.dylib arm64  <373b4d279e6432d5b718ec5b71aebfc4> /usr/lib/system/libsystem_blocks.dylib
0x18c2e4000 - 0x18c360fff libsystem_c.dylib arm64  <d31511075c1b38bcbc5198a7f40447b5> /usr/lib/system/libsystem_c.dylib
0x18c361000 - 0x18c365fff libsystem_configuration.dylib arm64  <1db4aaed5fdc3cd592a52f7358d1c666> /usr/lib/system/libsystem_configuration.dylib
0x18c366000 - 0x18c36bfff libsystem_containermanager.dylib arm64  <15235799c22434b78bfd0f93cdc2c9dc> /usr/lib/system/libsystem_containermanager.dylib
0x18c36c000 - 0x18c36dfff libsystem_coreservices.dylib arm64  <31d817e729333cd6be4695ade5abf990> /usr/lib/system/libsystem_coreservices.dylib
0x18c36e000 - 0x18c386fff libsystem_coretls.dylib arm64  <099dd5a82bed308882bc1782787b23cc> /usr/lib/system/libsystem_coretls.dylib
0x18c387000 - 0x18c38dfff libsystem_dnssd.dylib arm64  <58d80a29aee7360ab16718545b8102a2> /usr/lib/system/libsystem_dnssd.dylib
0x18c38e000 - 0x18c3b1fff libsystem_info.dylib arm64  <d0d5a77de46631fca60abd5313794ef1> /usr/lib/system/libsystem_info.dylib
0x18c3b2000 - 0x18c3d6fff libsystem_kernel.dylib arm64  <2ccf4db33c323a68b05942b8375b90c2> /usr/lib/system/libsystem_kernel.dylib
0x18c3d7000 - 0x18c403fff libsystem_m.dylib arm64  <d2b0172418503909a26678ae48b1269c> /usr/lib/system/libsystem_m.dylib
0x18c404000 - 0x18c41ffff libsystem_malloc.dylib arm64  <44978732283439fc92fff8e3ab817123> /usr/lib/system/libsystem_malloc.dylib
0x18c420000 - 0x18c479fff libsystem_network.dylib arm64  <e59c5c150b41309481d185ca548ec114> /usr/lib/system/libsystem_network.dylib
0x18c47a000 - 0x18c483fff libsystem_networkextension.dylib arm64  <4d2d53bd1d0133209b896e201160a682> /usr/lib/system/libsystem_networkextension.dylib
0x18c484000 - 0x18c48efff libsystem_notify.dylib arm64  <fb43e04c8d8e3001bd73370115b6abd4> /usr/lib/system/libsystem_notify.dylib
0x18c48f000 - 0x18c495fff libsystem_platform.dylib arm64  <021e2b400d1b36f1927cd8b9ef5771ff> /usr/lib/system/libsystem_platform.dylib
0x18c496000 - 0x18c49ffff libsystem_pthread.dylib arm64  <ec957ca38cdb3ff39a675b484d59d580> /usr/lib/system/libsystem_pthread.dylib
0x18c4a0000 - 0x18c4a3fff libsystem_sandbox.dylib arm64  <1a659aa7dc7f34d988fda8e46bbd67d6> /usr/lib/system/libsystem_sandbox.dylib
0x18c4a4000 - 0x18c4abfff libsystem_symptoms.dylib arm64  <29eb26c4ca5c3bd0aaf1f8bd8ce2e600> /usr/lib/system/libsystem_symptoms.dylib
0x18c4ac000 - 0x18c4befff libsystem_trace.dylib arm64  <a42d46c7e3463233b75873d1e3ac2267> /usr/lib/system/libsystem_trace.dylib
0x18c4bf000 - 0x18c4c4fff libunwind.dylib arm64  <0963fc28375630e68ccd844e4f48d1b2> /usr/lib/system/libunwind.dylib
0x18c4c5000 - 0x18c4c5fff libvminterpose.dylib arm64  <acccc912f98833088c662f78ec126fe2> /usr/lib/system/libvminterpose.dylib
0x18c4c6000 - 0x18c4ecfff libxpc.dylib arm64  <9bf3e86d19f1318a9b1906a2681cf234> /usr/lib/system/libxpc.dylib
0x18c4ed000 - 0x18c702fff libicucore.A.dylib arm64  <8784ed7062a139ad9c768ed801bb5c8f> /usr/lib/libicucore.A.dylib
0x18c703000 - 0x18c714fff libz.1.dylib arm64  <76ea48b2d8053291891a800d76088c09> /usr/lib/libz.1.dylib
0x18d2aa000 - 0x18d62bfff CoreFoundation arm64  <106dcfdae2ac31b9af16e54e3fdb49be> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
0x18d62c000 - 0x18d63cfff libbsm.0.dylib arm64  <e663bf7a74f43aad9f86229b0b29f376> /usr/lib/libbsm.0.dylib
0x18d63d000 - 0x18d63dfff libenergytrace.dylib arm64  <6ec005a9a0a931da96fff946b027ca37> /usr/lib/libenergytrace.dylib
0x18d63e000 - 0x18d6b9fff IOKit arm64  <04198d723e7f3834914ae5869c25e65c> /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
0x18d6ba000 - 0x18d6dafff libMobileGestalt.dylib arm64  <648fed3bf8af3ccdbd24e5c65e81ceb5> /usr/lib/libMobileGestalt.dylib
0x18d6db000 - 0x18d7c4fff libxml2.2.dylib arm64  <29f6e338c1f13348970811ca0f0fe293> /usr/lib/libxml2.2.dylib
0x18d7c5000 - 0x18d85ffff Security arm64  <2423134e64f939aba8368d62495f8197> /System/Library/Frameworks/Security.framework/Security
0x18d860000 - 0x18d8cbfff SystemConfiguration arm64  <e8aaab6905853f7e97fb492185cf20be> /System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration
0x18d8cc000 - 0x18d9dcfff libsqlite3.dylib arm64  <f1a568e393d531f6af5e1edb816cdab2> /usr/lib/libsqlite3.dylib
0x18d9dd000 - 0x18dd51fff CFNetwork arm64  <7074b3e719d23257b4cd53899121f5c8> /System/Library/Frameworks/CFNetwork.framework/CFNetwork
0x18dd52000 - 0x18dd5ffff libbz2.1.0.dylib arm64  <64376e53acd732f3b5c85ee50b4f01c1> /usr/lib/libbz2.1.0.dylib
0x18dd60000 - 0x18dd78fff liblzma.5.dylib arm64  <7b9227fb2acb3feda1bffd6427b92fd5> /usr/lib/liblzma.5.dylib
0x18dd79000 - 0x18dd93fff libCRFSuite.dylib arm64  <7c6afb4c2fb13be9a84cf6d72ce823d8> /usr/lib/libCRFSuite.dylib
0x18dd94000 - 0x18ddbdfff libarchive.2.dylib arm64  <d8f6218802123a0ca0ec0d922a65c9a6> /usr/lib/libarchive.2.dylib
0x18ddbe000 - 0x18ddbffff liblangid.dylib arm64  <c78b76c300b036c6852e9ff59b9b5e0a> /usr/lib/liblangid.dylib
0x18ddc0000 - 0x18e08ffff Foundation arm64  <73ff2b76d90f3c90b0108f6e36e3b71f> /System/Library/Frameworks/Foundation.framework/Foundation
0x18e090000 - 0x18e13bfff libBLAS.dylib arm64  <8efc2fffcc8d3817a73da84ffd232d46> /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/libBLAS.dylib
0x18e13c000 - 0x18e46afff libLAPACK.dylib arm64  <13c0d7676f6a381aa399570bf0142738> /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/libLAPACK.dylib
0x18e46b000 - 0x18e705fff vImage arm64  <8984ca1dbdd4341593c2532a3f112644> /System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/vImage
0x18e706000 - 0x18e72bfff libvMisc.dylib arm64  <1fc0b5b5a59c3ae78efa0a4124bf69b0> /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/libvMisc.dylib
0x18e72c000 - 0x18e740fff libLinearAlgebra.dylib arm64  <39992b5d7f8a38e7b46ab952e44aa2f2> /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/libLinearAlgebra.dylib
0x18e741000 - 0x18e752fff libSparseBLAS.dylib arm64  <b0af26c688c631508453fd8f08cf1d0b> /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/libSparseBLAS.dylib
0x18e753000 - 0x18e7c8fff libvDSP.dylib arm64  <dab772660509376eb918a2c72163797e> /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/libvDSP.dylib
0x18e7c9000 - 0x18e7c9fff vecLib arm64  <6c742a3f1ad83395a1a5f3fc1abd56f8> /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/vecLib
0x18e7ca000 - 0x18e7cafff Accelerate arm64  <166a50b815443ce89269964414cfd7b2> /System/Library/Frameworks/Accelerate.framework/Accelerate
0x18e7cb000 - 0x18ed0ffff CoreGraphics arm64  <f8a6e0de80b23cb8b41654b953606846> /System/Library/Frameworks/CoreGraphics.framework/CoreGraphics
0x18ed10000 - 0x18ed24fff GraphicsServices arm64  <93b597044b5234749061bb64ddf8adae> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
0x18ed25000 - 0x18ed72fff AppSupport arm64  <b9af4ec39608345594622765e98d4f8f> /System/Library/PrivateFrameworks/AppSupport.framework/AppSupport
0x18ed73000 - 0x18eea0fff MobileCoreServices arm64  <d07c54225af93b6289780c72418e3c9f> /System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices
0x18eea1000 - 0x18ef02fff BaseBoard arm64  <7aa95ea0660f325db0c7e793a3193cc5> /System/Library/PrivateFrameworks/BaseBoard.framework/BaseBoard
0x18ef03000 - 0x18ef12fff AssertionServices arm64  <4cef0d85a60b329e858109f9638abd7c> /System/Library/PrivateFrameworks/AssertionServices.framework/AssertionServices
0x18ef13000 - 0x18ef40fff BackBoardServices arm64  <322bd4e181fa3e77b99133e48e785e6c> /System/Library/PrivateFrameworks/BackBoardServices.framework/BackBoardServices
0x18ef41000 - 0x18ef44fff MobileSystemServices arm64  <97a4c37c60c337a88892f73e772573da> /System/Library/PrivateFrameworks/MobileSystemServices.framework/MobileSystemServices
0x18ef45000 - 0x18ef94fff FrontBoardServices arm64  <49204359a9843dd8ad769c64bc8f468f> /System/Library/PrivateFrameworks/FrontBoardServices.framework/FrontBoardServices
0x18ef98000 - 0x18efccfff SpringBoardServices arm64  <d573996d93083f05bf72c1ab6e9a64a1> /System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices
0x18efcd000 - 0x18efe7fff MobileKeyBag arm64  <f0cc77ecdbbd37b8a31aa6ea6107c2ae> /System/Library/PrivateFrameworks/MobileKeyBag.framework/MobileKeyBag
0x18efe8000 - 0x18eff0fff IOSurface arm64  <419bcf22d97732bd99f6f7bb6b50e133> /System/Library/PrivateFrameworks/IOSurface.framework/IOSurface
0x18eff1000 - 0x18effcfff liblockdown.dylib arm64  <e262bbe5419e3e5ba65b1bbe05144dcf> /usr/lib/liblockdown.dylib
0x18effd000 - 0x18f013fff CrashReporterSupport arm64  <c3b0e870e0ac38d892b690dab53f3306> 

It seems that the watchdog timer times out, I'm not exactly sure. Here is the code its executing:

-(void)burstModeCapture : (AVCaptureConnection *) videoConnection : (int) i{//start capturing picture s rapidly and cache them in ram

NSLog(@"time entering: %d", i);


[photoOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {// NO FLASH FIRST, THEN FLASH
     [flash turnOffFlash];
     NSLog(@"flash");
     if(error)
         NSLog(@"%s",[[error localizedDescription] UTF8String]);

     CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(imageSampleBuffer);
     CVPixelBufferLockBaseAddress(cameraFrame, 0);
     Byte *rawImageBytes = CVPixelBufferGetBaseAddress(cameraFrame);
     size_t bytesPerRow = CVPixelBufferGetBytesPerRow(cameraFrame);
     size_t width = CVPixelBufferGetWidth(cameraFrame);
     size_t height = CVPixelBufferGetHeight(cameraFrame);
     // Do whatever with your bytes

     // create suitable color space
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

     //Create suitable context (suitable for camera output setting kCVPixelFormatType_32BGRA)
     CGContextRef newContext = CGBitmapContextCreate(rawImageBytes, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

     CVPixelBufferUnlockBaseAddress(cameraFrame, 0);

     // release color space
     CGColorSpaceRelease(colorSpace);
     //Create a CGImageRef from the CVImageBufferRef
     CGImageRef newImage = CGBitmapContextCreateImage(newContext);
     UIImage *FinalImage = [[UIImage alloc] initWithCGImage:newImage];
     CGContextRelease(newContext);
     CFRelease(newImage);
     imagesArray[2-i] = [FinalImage copy];//append image to array

       shootCounter--;

         if (shootCounter <= 0) {
             [flash turnOffFlash];
             shootCounter = NUMSHOTS;
             UIImage *output = [self processImages];
             output = [imageProcessor rotateImg:output];
             output = [UI drawText:@"test" :output : CGPointMake([output size].width/2, [output size].height/2)];

             [UI updateUIWithOutput:output];
             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                 [self burstModeCapture: videoConnection : shootCounter];
             });
         }
         else {
             if (inputDevice.torchMode == AVCaptureTorchModeOn){
                 [flash turnOffFlash];
             }
             else{
                 [flash turnOnFlash];//SHOULD BE ON, OFF FOR TESTING PURPOSES
             }
             [NSThread sleepForTimeInterval: 0.1];
             [self burstModeCapture: videoConnection : shootCounter];
         }

     }];


}


- (IBAction)startCapture:(id)sender { // start capturing process when button is held down
    _Capturebtn.userInteractionEnabled = NO;
    //initialize semaphore to synchronize two threads

    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [photoOutput connections])
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection)
        {
            break;
        }
    }
        //run next code in background thread:
    [self shoot: [NSNumber numberWithInteger:NUMSHOTS] : videoConnection]; //capture is done for continous repeat this method
        //end of background thread

        //run on main thread:
    _Capturebtn.userInteractionEnabled = YES;

}

- (void)shoot:(NSNumber *)counter : (AVCaptureConnection *) videoConnection {
    int n = [counter intValue];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self burstModeCapture: videoConnection : n];
    });

}

It seems that completionHandler defaults to main thread. Again the code works, but fails after a while, any help would be really appreciated!