title: Rare crash during dealloc AVCaptureVideoPreviewLayer
body: <p>On client phones rarely can be replicated crash during camera dealloc</p>
  
  <pre><code>Fatal Exception: NSRangeException
  Cannot remove an observer &lt;AVCaptureSession 0x174212170&gt; for the key path "changeSeed" from &lt;AVCaptureConnection 0x17420fa60&gt; because it is not registered as an observer.
  
  Thread : Fatal Exception: NSRangeException
  0  CoreFoundation                 0x000000018449259c __exceptionPreprocess + 132
  1  libobjc.A.dylib                0x0000000194be40e4 objc_exception_throw + 60
  2  CoreFoundation                 0x00000001844924dc -[NSException initWithCoder:]
  3  Foundation                     0x00000001852a7e9c -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 528
  4  Foundation                     0x00000001852a7954 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 104
  5  AVFoundation                   0x0000000182d21054 -[AVCaptureSession _removeConnection:] + 192
  6  AVFoundation                   0x0000000182d206dc -[AVCaptureSession _removeVideoPreviewLayer:] + 120
  7  AVFoundation                   0x0000000182d300f8 -[AVCaptureVideoPreviewLayer dealloc] + 92
  </code></pre>
  
  <p>For stop capture session following code is used:
  All operation with session are precessed in background queue, as stopRunning can take some time</p>
  
  <pre><code>deinit {
    if let session = self.captureSession {
       dispatch_async(self.cameraQueue, { () -&gt; Void in
          session.beginConfiguration()
          let allInputs = session.inputs as! [AVCaptureInput]
          for input in allInputs {
             session.removeInput(input)
          }
          let allOutputs = session.outputs as! [AVCaptureOutput]
          for output in allOutputs {
             session.removeOutput(output)
          }
          session.commitConfiguration()
          session.stopRunning()
       })
    }
  }
  </code></pre>
  
  <p>Has anybody seen this crash before?</p>