-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
How can I create a save view into external memory through TypedData
?
#48704
Comments
TypeData
?TypedData
?
Perhaps @dcharkes would know this? Not sure if this relates to dart:ffi or not. |
Yes, but you need to take care to hold on to the object which has the finalizer. Indeed, if you use the bytebuffer to make a new typed data and lose the old one, the finalizer could run. Also, to ensure objects don't get collected prematurely you can add a Dart method to keeps it alive.
In Dart 2.17 we make using native finalizers much easier. First, we make them available in Dart code, rather than using the Dart API. sdk/sdk/lib/ffi/native_finalizer.dart Lines 232 to 297 in 532c116
Note that this only works for Secondly, we introduce sdk/sdk/lib/ffi/native_finalizer.dart Lines 7 to 223 in 532c116
And thirdly, we standardize on using a wrapper class wrapping the sdk/tests/ffi/vmspecific_native_finalizer_test.dart Lines 40 to 71 in 532c116
If you're using Dart 2.16 you can adopt the same patterns by wrapping your It should be fairly easy to migrate this to the |
Thanks for the detailed response! In my use cases I have another API which takes a As an aside, is there a difference between these two reachability fences? @pragma('vm:never-inline')
void reachabilityFence(Object? object) {} @pragma('vm:never-inline')
Object? reachabilityFence(Object? obj) {
return obj;
} I was using the first variant for the longest time but got crashes in AOT mode which looked like being caused by premature finalization of the native resource. I'm not seeing those crashes with the second variant. |
That is very curious. I would have to investigate that.
Unfortunately that is currently not supported. I'll give it some thought. Maybe it would be possible to convert a |
I've dug some deeper on this: And it looks like it is actually safe to use the buffers. The buffer wraps the typed data:
And getting another TypedData creates a view around the same backing store: sdk/sdk/lib/_internal/vm/lib/typed_data_patch.dart Lines 1914 to 1920 in 6faa5f3
The sdk/runtime/vm/dart_api_impl.cc Lines 3808 to 3813 in 6faa5f3
So, doing |
Thanks for looking into this. That solves the issue for me. |
I have some externally allocated memory and would like to allow code to access it without having to know about the lifetime of the memory. I can get a
Uint8List
throughPointer.asTypedList
but I have to manually manage the lifetime of the backing memory.Is it safe to attach a native finalizer to the object returned from
Pointer.asTypedList
, which frees the external memory, throughDart_NewFinalizableHandle
?Also, what would be the implications for the
Uint8List
'sByteBuffer
? I suspect it would not be safe to use it.The text was updated successfully, but these errors were encountered: