Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vm/ffi] NativeFinalizers with out parameters #54923

Closed
dcharkes opened this issue Feb 15, 2024 · 2 comments
Closed

[vm/ffi] NativeFinalizers with out parameters #54923

dcharkes opened this issue Feb 15, 2024 · 2 comments
Labels
area-documentation Prefer using 'type-documentation' and a specific area label. area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi

Comments

@dcharkes
Copy link
Contributor

Some or close/release functions have a different signature. If such signature requires extra parameters being passed in, such functions can be wrapped in a C function that takes a pointer to a helper struct that has the arguments that should be passed into the finalizer function. One such example would be HeapFree in the windows API.

However, some native finalization functions have out parameters that communicate error messages. Because native finalization functions cannot call back into Dart, the only way to communicate such information to Dart is to use an NativeCallable.listener to communicate the errors back to the isolate. Since finalizers are run async anyway, I believe this is a reasonable thing to do.

Any other ideas on how to deal with this @mkustermann? Or do you think scheduling something on the Dart event loop with NativeCallable.listener is the right thing to do?

We should probably add some documentation about how to do this.

@dcharkes dcharkes added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. area-documentation Prefer using 'type-documentation' and a specific area label. library-ffi labels Feb 15, 2024
@dcharkes
Copy link
Contributor Author

Oh calling NativeCallable.listener from a NativeFinalizer doesn't work yet. I've filed: #54939.

I've made an example on how this is supposed to work in: https://github.com/dart-lang/native/tree/native-finalizer/pkgs/native_assets_cli/example/native_add_library. We can clean it up and land it as a sample later for documentation purposes.

@dcharkes
Copy link
Contributor Author

As discussed offline with @liamappelbe and @mkustermann:

  1. Resources should not be freed in Dart code. (Not the use case mentioned above.) When the isolate shuts down that would lead to resources not being freed.
  2. Errors should not be reported via a Dart callback. (The use case above.) That would swallow error messages if the isolate is shut down. Instead the errors should be reported via whatever native error reporting API is available. For example: https://developer.android.com/reference/android/util/Log and https://developer.apple.com/documentation/os/logging.

@dcharkes dcharkes closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-documentation Prefer using 'type-documentation' and a specific area label. area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi
Projects
None yet
Development

No branches or pull requests

1 participant