Ionic tab bar app with barcode scanner crashes on iOS real device when change from scanner to second page

I have an app with two tabs one is settings page and the other is QR Code scanner when I move from the scanner page to settings page app crashes.

Also, the problem looks to appear almost every time on newer devices.
It’s hard to replicate it on iPhone 5s but more common on iPhone 8 and almost every time on iPhone 12

packaga.json

{
  "dependencies": {
    "@awesome-cordova-plugins/core": "^5.36.1",
    "@awesome-cordova-plugins/fingerprint-aio": "^5.36.0",
    "@awesome-cordova-plugins/native-storage": "^5.36.0",
    "@capacitor-community/barcode-scanner": "^2.0.1",
    "@capacitor/android": "3.2.2",
    "@capacitor/app": "1.0.3",
    "@capacitor/clipboard": "^1.0.3",
    "@capacitor/core": "3.2.2",
    "@capacitor/haptics": "1.0.3",
    "@capacitor/ios": "^3.2.2",
    "@capacitor/keyboard": "1.0.3",
    "@capacitor/network": "^1.0.3",
    "@capacitor/push-notifications": "^1.0.4",
    "@capacitor/share": "^1.0.4",
    "@capacitor/splash-screen": "^1.1.2",
    "@capacitor/status-bar": "1.0.3",
    "@ionic/react": "^5.7.0",
    "@ionic/react-router": "^5.7.0",
    "@ionic/storage": "^3.0.6",
    "aes256": "^1.1.0",
    "cordova-plugin-fingerprint-aio": "^4.0.2",
    "cordova-plugin-nativestorage": "^2.3.2",
    "es6-promise-plugin": "^4.2.2",
    "i18next": "^20.4.0",
    "i18next-resources-to-backend": "^1.0.0",
    "ionicons": "^5.4.0",
    "jetifier": "^2.0.0",
    "node-sass": "^6.0.1",
    "qrcode.react": "^1.0.1",
    "react": "^17.0.1",
    "react-circular-progressbar": "^2.0.4",
    "react-dom": "^17.0.1",
    "react-hook-form": "^7.15.0",
    "react-i18next": "^11.11.4",
    "react-router": "^5.2.1",
    "react-router-dom": "^5.3.0",
    "react-scripts": "4.0.3",
    "swiper": "^6.8.4"
  },
  "devDependencies": {
    "@capacitor/cli": "3.1.2",
    // ..............
    "rxjs": "^6.6.7",
    "typescript": "^4.1.3"
  },
}

scan page

const ScanPage: React.FC = () => {
  const { t } = useTranslation();
  const { checkAuth } = useAuth();
  const { startScanner, stopScanner, showLoading, loaderMessage } = useCamera();

  useIonViewDidEnter(async () => {
    await checkAuth();
    startScanner();
  });
  useIonViewDidLeave(() => stopScanner());

  return (
   // ..................
  );
};

useCamera hook (custom hook with reusable functions)

import { BarcodeScanner } from "@capacitor-community/barcode-scanner";

const useCamera = () => {
  const didUserGrantAccess = async () => {
    await BarcodeScanner.prepare();
    const status = await BarcodeScanner.checkPermission({ force: true });

    if (status.granted) {
      return startScanner();
    } else {
      if (confirm(t("You have denied camera permission! You can allow camera access in your settings."))){
        return BarcodeScanner.openAppSettings();
      }
    }
  };

  const handleActionApprove = async (pass: string) => {
    // ..................
    scanResult.current = null;
    fromCameraRef.current = false;
    setTimeout(() => startScanner(), 300);

    // ..................
  };

  const startActionProcess = (actionUrl: string, fromCamera = false): void => {
      // ..................
  };

  const startScanner = async () => {
    const status = await BarcodeScanner.checkPermission();

    if (status.granted) {
      BarcodeScanner.hideBackground(); // make background of WebView transparent

      const result = await BarcodeScanner.startScan(); // start scanning and wait for a result

      if (result.hasContent && result.content) {
        logger("scan result", result.content);
        startActionProcess(result.content, true);
      }
    } else {
      didUserGrantAccess();
    }
  };

  const stopScanner = (): void => {
    BarcodeScanner.showBackground();
    BarcodeScanner.stopScan();
  };
};

and this is the crash report it seems to be something with the stopping qr scanner
(this is the first part of the report which has more info)

Hardware Model:      iPhone13,2
Identifier:          ...............
Version:             2.1 (1)
AppVariant:          1:iPhone13,2:15
Beta:                YES
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           ........

Date/Time:           2022-04-25 08:52:11.2641 +0300
Launch Time:         2022-04-25 08:52:07.2923 +0300
OS Version:          iPhone OS 15.4.1 (19E258)
Release Type:        User
Baseband Version:    2.53.01
Report Version:      104

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x0000000102de466c
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: SIGNAL 5 Trace/BPT trap: 5
Terminating Process: exc handler [60136]

Triggered by Thread:  0


Thread 0 name:
Thread 0 Crashed:
0   CapacitorCommunityBarcodeScanner	0x0000000102de466c partial apply for closure #1 in BarcodeScanner.dismantleCamera() + 252 (Plugin.swift:0)
1   CapacitorCommunityBarcodeScanner	0x0000000102de014c thunk for @escaping @callee_guaranteed () -> () + 20 (<compiler-generated>:0)
2   libdispatch.dylib             	0x00000001a065ae68 _dispatch_call_block_and_release + 32 (init.c:1517)
3   libdispatch.dylib             	0x00000001a065ca2c _dispatch_client_callout + 20 (object.m:560)
4   libdispatch.dylib             	0x00000001a066af48 _dispatch_main_queue_drain + 928 (inline_internal.h:2622)
5   libdispatch.dylib             	0x00000001a066ab98 _dispatch_main_queue_callback_4CF + 44 (queue.c:7770)
6   CoreFoundation                	0x00000001a09ae2f0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1795)
7   CoreFoundation                	0x00000001a09681f4 __CFRunLoopRun + 2532 (CFRunLoop.c:3144)
8   CoreFoundation                	0x00000001a097b6b8 CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
9   GraphicsServices              	0x00000001bca15374 GSEventRunModal + 164 (GSEvent.c:2200)
10  UIKitCore                     	0x00000001a32e0e88 -[UIApplication _run] + 1100 (UIApplication.m:3511)
11  UIKitCore                     	0x00000001a30625ec UIApplicationMain + 364 (UIApplication.m:5064)
12  App                           	0x0000000102b61fec main + 64 (AppDelegate.swift:6)
13  dyld                          	0x0000000102c91ce4 start + 520 (dyldMain.cpp:879)

Have you tried calling stopScanner in ionViewWillLeave or componentWillUnmount?

I’m calling it but in useIonViewDidLeave(() => stopScanner());

Right, have you tried the other two methods? I am thinking maybe with DidLeave the scanner object reference has been destroyed so it is throwing an error.