dispose method

Future<void> dispose()

Releases all resources held by the detector.

After calling dispose, you must call initialize again before running any detections.

Implementation

Future<void> dispose() async {
  // Flip ALL ready-state synchronously (before any await) so an un-awaited
  // dispose() immediately reports not-ready: a later call throws StateError
  // via the _require* guards instead of a null-check TypeError on a
  // half-torn-down handle, and a second dispose() is a no-op rather than
  // disposing the same worker twice.
  final segmentationRpc = _segmentationRpc;
  final worker = _worker;
  _segmentationRpc = null;
  _segmentationInitialized = false;
  _worker = null;

  await segmentationRpc?.disposeGracefully(disposeOp: 'dispose');
  if (worker != null && worker.isReady) {
    // Graceful shutdown: await the isolate's dispose ack before force-killing
    // so it can free its native TFLite interpreters instead of being reaped
    // mid-cleanup by Isolate.kill(priority: immediate).
    await worker.disposeGracefully();
  }
}