heremaps / gluecodium

Cross-language bindings generator for C++, Java, Swift, and Dart
Apache License 2.0
202 stars 27 forks source link

Investigate support for automatic finalizers in Dart #744

Closed DanielKamkha closed 3 years ago

DanielKamkha commented 3 years ago

According to https://github.com/dart-lang/sdk/issues/35770, Dart finalizers are available on FFI level (not yet available on Dart language level). This might be enough to implement automatic cleanup of C++ objects in Gluecodium Dart bindings.

If this proves to be successful, release() methods on generated Dart objects should be made a no-op and marked as deprecated.

DanielKamkha commented 3 years ago

Current FFI-level finaliziers support requires compiling the FFI binary while explicitly including "dart_api_dl.cc" ("dart_api_dl.c" starting from Dart 2.12) as a source. This file is available from a public "include" directory of Dart SDK for Dart 2.9+.

However this file is not available if Dart SDK is installed as a package. This could be probably alleviated by obtaining this file as a single file from Dart SDK public repo, without the need of downloading full SDK source.

DanielKamkha commented 3 years ago

Dart 2.12 makes a breaking change the finalizers FFI API (https://github.com/dart-lang/sdk/issues/42312). It makes weak persistent handles to not auto-delete anymore, and introduces new handle type Dart_FinalizableHandle that auto-deletes instead. The signature of FFI finalizers also changes and so Gluecodium templates would need to be adjusted to match.

DanielKamkha commented 3 years ago

Usage of the Dart DL FFI API for weak handles and finalizers was proven to be feasible. However, due to a breaking change of that API coming up in Dart 2.12 we're postponing usage of this API in Gluecodium-generated code until Dart 2.12 is released to stable release channel.

Spike code, updated to Dart 2.12, is shelved in this branch: https://github.com/heremaps/gluecodium/tree/spike-dart-finalizers