checkNativeStatus function

void checkNativeStatus(
  1. int call(
    1. Pointer<Pointer<Char>> error
    ),
  2. void errorFree(
    1. Pointer<Char> message
    ), {
  3. Object failure(
    1. String message,
    2. int status
    )?,
})

Runs call, one of Google's C functions that report a failure through an error out-parameter, and throws a failure as a TaskException with Google's message and status, or as what failure makes of them. errorFree is the calling family's MpErrorFree, since each family loads its own library; it frees Google's message on every path.

Implementation

void checkNativeStatus(
  int Function(Pointer<Pointer<Char>> error) call,
  void Function(Pointer<Char> message) errorFree, {
  Object Function(String message, int status)? failure,
}) => using((arena) {
  final error = arena<Pointer<Char>>();
  try {
    final status = call(error);
    if (status != 0) {
      final message =
          nativeString(error.value) ?? 'MediaPipe failed with status $status.';
      throw failure?.call(message, status) ??
          TaskException(message, statusCode: status);
    }
  } finally {
    if (error.value != nullptr) errorFree(error.value);
  }
});