flutter-ml / google_ml_kit_flutter

A flutter plugin that implements Google's standalone ML Kit
MIT License
915 stars 708 forks source link

iOS crash due to NSInvalidArgumentException in GTMSessionFetcher/MLKITx_CCTClearcutUploader #580

Open xonaman opened 5 months ago

xonaman commented 5 months ago

Hello,

I am experiencing a crash on iOS after running the following code a few times. From the logs it seems like the error come from the core, that’s why I post this here.

The code works completely fine, the crash appeared where ImageLabeler and TextRecognizer were already disposed for some time.

My Flutter Code

class _GoogleMlKitTestScreen extends StatefulWidget {
  final String imagePath;

  const _GoogleMlKitTestScreen({required this.imagePath});

  @override
  _GoogleMlKitTestScreenState createState() => _GoogleMlKitTestScreenState();
}

class _GoogleMlKitTestScreenState extends State<_GoogleMlKitTestScreen> {
  List<String>? _typeLabels;
  List<String>? _textBlocks;

  @override
  void initState() {
    super.initState();
    _extractImageData();
  }

  @override
  void dispose() {
    _typeLabels?.clear();
    _textBlocks?.clear();
    super.dispose();
  }

  Future<void> _extractImageData() async {
    final InputImage image = InputImage.fromFilePath(widget.imagePath);

    final ImageLabeler labeler = ImageLabeler(options: ImageLabelerOptions());
    final List<ImageLabel> labels = await labeler.processImage(image);
    await labeler.close();

    final TextRecognizer recognizer = TextRecognizer();
    final RecognizedText text = await recognizer.processImage(image);
    await recognizer.close();

    if (mounted) {
      setState(() {
        _typeLabels = labels.map((ImageLabel label) => label.label).toList();
        _textBlocks = text.blocks.map((TextBlock block) => block.text).toList();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    // [...]
  }
}

My Flutter Version

Flutter 3.19.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision bae5e49bc2 (7 days ago) • 2024-02-13 17:46:18 -0800
Engine • revision 04817c99c9
Tools • Dart 3.3.0 • DevTools 2.31.1

Device used

Logs

Initialized TensorFlow Lite runtime.
INFO: Initialized TensorFlow Lite runtime.
Created TensorFlow Lite XNNPACK delegate for CPU.
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Initialized TensorFlow Lite runtime.
INFO: Initialized TensorFlow Lite runtime.
Created TensorFlow Lite XNNPACK delegate for CPU.
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean objectForKey:]: unrecognized selector sent to instance 0x1169c9f78'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001165d4761 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x0000000115070904 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001165e9873 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   CoreFoundation                      0x00000001165d8ef4 ___forwarding___ + 1459
    4   CoreFoundation                      0x00000001165db0f8 _CF_forwarding_prep_0 + 120
    5   GTMSessionFetcher                   0x0000000112d6bf9c __46+[GTMSessionFetcher appAllowsInsecureRequests]_block_invoke + 108
    6   libdispatch.dylib                   0x00000001163a69f7 _dispatch_client_callout + 8
    7   libdispatch.dylib                   0x00000001163a8095 _dispatch_once_callout + 66
    8   GTMSessionFetcher                   0x0000000112d6befe +[GTMSessionFetcher appAllowsInsecureRequests] + 78
    9   GTMSessionFetcher                   0x0000000112d6dab5 -[GTMSessionFetcher beginFetchMayDelay:mayAuthorize:mayDecorate:] + 1941
    10  GTMSessionFetcher                   0x0000000112d6c740 -[GTMSessionFetcher beginFetchWithCompletionHandler:] + 304
    11  Runner                              0x0000000100fac99e -[MLKITx_CCTClearcutUploader uploadBatch:files:] + 1696
    12  Runner                              0x0000000100fab7f3 -[MLKITx_CCTClearcutUploader uploadNext] + 384
    13  Runner                              0x0000000100faa670 __73-[MLKITx_CCTClearcutUploader uploadWithCompletionHandler:isOnForeground:]_block_invoke.140 + 1580
    14  libdispatch.dylib                   0x00000001163a5747 _dispatch_call_block_and_release + 12
    15  libdispatch.dylib                   0x00000001163a69f7 _dispatch_client_callout + 8
    16  libdispatch.dylib                   0x00000001163ae8c9 _dispatch_lane_serial_drain + 1127
    17  libdispatch.dylib                   0x00000001163af665 _dispatch_lane_invoke + 441
    18  libdispatch.dylib                   0x00000001163bc76e _dispatch_root_queue_drain_deferred_wlh + 318
    19  libdispatch.dylib                   0x00000001163bbb69 _dispatch_workloop_worker_thread + 590
    20  libsystem_pthread.dylib             0x0000000116e1ac47 _pthread_wqthread + 327
    21  libsystem_pthread.dylib             0x0000000116e19b97 start_wqthread + 15
)
libc++abi: terminating due to uncaught exception of type NSException