withZoneArena<R> function

R withZoneArena<R>(
  1. R computation(), [
  2. Allocator wrappedAllocator = calloc
])

Creates a zoned Arena to manage native resources.

The arena is availabe through zoneArena.

If the isolate is shut down, through Isolate.kill(), resources are not cleaned up.

Implementation

R withZoneArena<R>(R Function() computation,
    [Allocator wrappedAllocator = calloc]) {
  final arena = Arena(wrappedAllocator);
  var arenaHolder = [arena];
  bool isAsync = false;
  try {
    return runZoned(() {
      final result = computation();
      if (result is Future) {
        isAsync = true;
        return result.whenComplete(() {
          arena.releaseAll();
        }) as R;
      }
      return result;
    }, zoneValues: {#_arena: arenaHolder});
  } finally {
    if (!isAsync) {
      arena.releaseAll();
      arenaHolder.clear();
    }
  }
}