AVFoundation: Strange error while trying to switch camera formats with the touch of a single button.

I'm getting the following output from my iOS app's debug console, note the error on the last line:

Capture format keys: ["600x600@25", "1200x1200@5", "1200x1200@30", "1600x1200@2", "1600x1200@30", "3200x2400@15", "3200x2400@2", "600x600@30"]
Start capture session for 1600x1200@30: <AVCaptureSession: 0x303c70190 [AVCaptureSessionPresetPhoto]>
Stop capture session: <AVCaptureSession: 0x303c70190 [AVCaptureSessionPresetInputPriority]>
	<AVCaptureDeviceInput: 0x303ebb720 [Medwand S3 Camera]>[vide] -> <AVCaptureVideoDataOutput: 0x303edf1e0>
	<AVCaptureDeviceInput: 0x303ebb720 [Medwand S3 Camera]>[vide] -> <AVCapturePhotoOutput: 0x303ee3e20>
	<AVCaptureDeviceInput: 0x303ebb720 [Medwand S3 Camera]>[vide] -> <AVCaptureVideoPreviewLayer: 0x3030b33c0>
Start capture session for 600x600@30: <AVCaptureSession: 0x303c70190 [AVCaptureSessionPresetInputPriority]>
	<AVCaptureDeviceInput: 0x303ebb720 [Medwand S3 Camera]>[vide] -> <AVCaptureVideoDataOutput: 0x303edf1e0>
	<AVCaptureDeviceInput: 0x303ebb720 [Medwand S3 Camera]>[vide] -> <AVCapturePhotoOutput: 0x303ee3e20>
	<AVCaptureDeviceInput: 0x303ebb720 [Medwand S3 Camera]>[vide] -> <AVCaptureVideoPreviewLayer: 0x3030b33c0>
<<<< FigSharedMemPool >>>> Fig assert: "blkHdr->useCount > 0" at  (FigSharedMemPool.c:591) - (err=0)

This is in response to trying to switch capture formats between the two key modes that must regularly be used by my application. Below you will find the functions that I use to start and stop capturing frames to my preview layer. I have a UI with three buttons.

  1. Off
  2. Mode 1
  3. Mode 2

If I tap the Off button in between tapping Mode 1 or Mode 2 all is well; I can do this all day. However, attempt to jump between Mode 1andMode 2` directly I run into issues. I added a layer of software between the UI and the underlying functions so that I could make sure to turn off the Camera before turning it back on in the opposite mode and was surprised to get this output. Can someone at Apple please tell me what is going on here?

For the rest of you, if anyone knows the magic incantation to safely switch camera formats, please paste that code here. Thanks. I've included my code below.

    func start(for deviceFormat: String) {
        sessionQueue.async { [unowned self] in
            logger.debug("Start capture session for \(deviceFormat): \(self.captureSession)")

            do {
                guard let format = formatDict[deviceFormat] else { throw Error.captureFormatNotFound }
                captureSession.stopRunning()
                captureSession.beginConfiguration()         // May not be necessary.
                try captureDevice.lockForConfiguration()    // Without this we get an error.
                captureDevice.activeFormat = format
                captureDevice.unlockForConfiguration()      // Matching function: Necessary.
                captureSession.commitConfiguration()        // Matching function: May not be necessary.
                captureSession.startRunning()
            } catch {
                logger.fault("Failed to start camera: \(error.localizedDescription)")
                errorPublisher.send(error)
            }
        }
    }

    func stop() {
        sessionQueue.async { [unowned self] in
            logger.debug("Stop capture session: \(self.captureSession)")
            captureSession.stopRunning()
        }
    }
AVFoundation: Strange error while trying to switch camera formats with the touch of a single button.
 
 
Q