I'm implementing a library that uses `dart:ffi` to bind functions of `PDFium` in a object oriented manner. Until now, it is quite easy to translate the C functions into dart and hide the whole pointer stuff behind dispose functions and classes.

Now I want to implement the search part and I kind of want to use a generator (sync or async) to create an `Iterable<...>`.

The question is now: is it possible to provide some kind of "cleanup code" for a generator function? If the iterator that is returned never completes, it creates memory leaks. Thus, I need a way to cleanup allocated resources *after* the iterator (or stream) completes.

```dart
class Foo {
  Iterable<dynamic> search() sync* {
    var page = getPagePointer();
    for result in page.search() {
      yield result;
    }
    closePagePointer(page); // <-- how can I guarantee this?
  }
}

```

Maybe there is no way of guaranteeing this, then I will not create a stream/iterator but it would be a nice use-case for a generator function.

Thank you for any help!