App crashes randomly in observeValueForKeyPath - iOS AVCaptureFigVideoDevice

Hi,

Many users of my app are having crash problems. It happens or during the opening of the app or during the use.

I'm unable to replicate the error because the problem does not occur on my devices, and I'm not managing to find a solution.

Most users will solve the problem by reinstalling the app.

In the crashlog the method that causes the bug is `[AVCaptureFigVideoDevice temperatureAndTintValuesForDeviceWhiteBalanceGains:]` called in `[ViewController observeValueForKeyPath:ofObject:change:context:]` but sometimes the crash occurs in other line of the method `observeValueForKeyPath`.

So I think the problem is caused by the observers.

I add the observer in the viewWillAppear and remove on viewDidDisappear


   - (void)addObservers
    {
        [self addObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:SessionRunningAndDeviceAuthorizedContext];
        [self addObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:CapturingStillImageContext];
        [self addObserver:self forKeyPath:@"movieFileOutput.recording" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:RecordingContext];
       
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.focusMode" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:FocusModeContext];
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.lensPosition" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:LensPositionContext];
       
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.exposureMode" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:ExposureModeContext];
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.exposureDuration" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:ExposureDurationContext];
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.ISO" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:ISOContext];
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.exposureTargetOffset" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:ExposureTargetOffsetContext];
       
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.whiteBalanceMode" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:WhiteBalanceModeContext];
        [self addObserver:self forKeyPath:@"videoDeviceInput.device.deviceWhiteBalanceGains" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:DeviceWhiteBalanceGainsContext];
       
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[self videoDevice]];
       
    }


    - (void)removeObservers
    {
      [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[self videoDevice]];
      [[NSNotificationCenter defaultCenter] removeObserver:[self runtimeErrorHandlingObserver]];
     
      [self removeObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" context:SessionRunningAndDeviceAuthorizedContext];
      [self removeObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" context:CapturingStillImageContext];
      [self removeObserver:self forKeyPath:@"movieFileOutput.recording" context:RecordingContext];
     
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.focusMode" context:FocusModeContext];
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.lensPosition" context:LensPositionContext];
     
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.exposureMode" context:ExposureModeContext];
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.exposureDuration" context:ExposureDurationContext];
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.ISO" context:ISOContext];
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.exposureTargetOffset" context:ExposureTargetOffsetContext];
     
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.whiteBalanceMode" context:WhiteBalanceModeContext];
      [self removeObserver:self forKeyPath:@"videoDeviceInput.device.deviceWhiteBalanceGains" context:DeviceWhiteBalanceGainsContext];
   
    }






Here's a crashlog:



Last Exception Backtrace:
    0   CoreFoundation                 0x19df7bab0 __exceptionPreprocess + 224
    1   libobjc.A.dylib               0x19dc95028 objc_exception_throw + 59
    2   AVFoundation                   0x1a82ed658 -[AVCaptureFigVideoDevice temperatureAndTintValuesForDeviceWhiteBalanceGains:] + 263
    3   Reflex                         0x1002b6b90 __65-[ViewController observeValueForKeyPath:ofObject:change:context:]_block_invoke + 633744 (ViewController.m:5483)
    4   libdispatch.dylib             0x19dc20b7c _dispatch_call_block_and_release + 31
    5   libdispatch.dylib             0x19dc21fd8 _dispatch_client_callout + 19
    6   libdispatch.dylib             0x19dc2dcc8 _dispatch_main_queue_callback_4CF + 967
    7   CoreFoundation                 0x19def6e0c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 15
    8   CoreFoundation                 0x19def1b68 __CFRunLoopRun + 1979
    9   CoreFoundation                 0x19def1084 CFRunLoopRunSpecific + 479
    10  GraphicsServices               0x1a813f534 GSEventRunModal + 107
    11  UIKitCore                     0x1a2061670 UIApplicationMain + 1939
    12  App                         0x100350e6c main + 1265260 (main.m:14)
    13  libdyld.dylib                 0x19dd70e18 start + 3
   
   
    Thread 0 name:  Dispatch queue: com.apple.main-thread
    Thread 0 Crashed:
    0   libsystem_kernel.dylib         0x000000019dd66efc __pthread_kill + 8
    1   libsystem_pthread.dylib       0x000000019dc86d10 pthread_kill + 196
    2   libsystem_c.dylib             0x000000019dc16a74 abort + 104
    3   libc++abi.dylib               0x000000019dd2e3c8 __cxa_bad_cast + 0
    4   libc++abi.dylib               0x000000019dd2e5c0 demangling_unexpected_handler+ 5568 () + 0
    5   libobjc.A.dylib               0x000000019dc95308 _objc_terminate+ 25352 () + 124
    6   libc++abi.dylib               0x000000019dd3b634 std::__terminate(void (*)+ 58932 ()) + 20
    7   libc++abi.dylib               0x000000019dd3b5c0 std::terminate+ 58816 () + 44
    8   libdispatch.dylib             0x000000019dc21fec _dispatch_client_callout + 40
    9   libdispatch.dylib             0x000000019dc2dcc8 _dispatch_main_queue_callback_4CF + 968
    10  CoreFoundation                 0x000000019def6e0c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
    11  CoreFoundation                 0x000000019def1b68 __CFRunLoopRun + 1980
    12  CoreFoundation                 0x000000019def1084 CFRunLoopRunSpecific + 480
    13  GraphicsServices               0x00000001a813f534 GSEventRunModal + 108
    14  UIKitCore                     0x00000001a2061670 UIApplicationMain + 1940
    15  App                         0x0000000100350e6c main + 1265260 (main.m:14)
    16  libdyld.dylib                 0x000000019dd70e18 start + 4