Comments (7)
You can use .asTypedList(int length)
to get a List<int>
view on native memory.
https://api.dartlang.org/stable/2.6.0/dart-ffi/Uint8Pointer/asTypedList.html
The other way around always requires writing a loop to copy the contents from the Dart list (in Dart garbage collected memory) to C memory.
Feel free to write helper methods/extensions for that and make a pull request to this repo.
from ffi.
@dcharkes Thanks for the hints! but it's still not very clear to me whether dart VM is able to GC the result of asTypedList(int length) if it's a direct view on native memory (some kind of weak reference on dart side?) , or is it good practice to always make a copy to a strong-ref List in dart and free the native memory ASAP to avoid memory leak.
from ffi.
Yes, that is true.
I've been working on some pool abstraction that you can use for memory management, see https://dart-review.googlesource.com/c/sdk/+/123662. I hope to land (a variation) of that in this package. Does that cover your use case?
If not, you can also write copy helpers for copying out the data from C to Dart.
from ffi.
it's still not very clear to me whether dart VM is able to GC the result of asTypedList(int length)
At this point no.
I've created a Pool abstraction for memory management, please see #30. You can use this to call free.
Finalizers will take longer to implement, please see dart-lang/sdk#35770. When we've added those, you can also use that to call free.
I'll close this issue, please reopen if you need more help.
from ffi.
Right now, that store operation will just do a loop of Pointer<Uint8>
loads and stores. Making that an actual memcopy is tracked in dart-lang/sdk#43967.
from ffi.
Hey, I would like to convert the dart list to a c-pointer so as to pass them in the c-code. Is there any specific function? Can you tell me the procedure to copy contents from the dart list to the c-pointer?
from ffi.
This sounds like a stack overflow question.
Most likely something such as:
import "package:ffi/ffi.dart";
final myList = [1, 2, 3, 4];
final myPointer = malloc<Int64>(myList.length);
for (int index = 0; index < myList.length; index++) {
myPointer[index] = myList[index];
}
// Use `myPointer`.
malloc.free(myPointer);
Or
import "package:ffi/ffi.dart";
final myList = [1, 2, 3, 4];
using((arena) {
final myPointer = arena<Int64>(myList.length);
for (int index = 0; index < myList.length; index++) {
myPointer[index] = myList[index];
}
// Use `myPointer`.
});
from ffi.
Related Issues (20)
- Setup automated publishing
- Unable to load .so library in flutter using platform android emulator HOT 4
- unable to load .dylib in flutter app on ios platform HOT 1
- Unload / Close librarie HOT 2
- Finalizing typed list to free its native memory HOT 2
- Rename `master` to `main` HOT 4
- Convert allocation to `@Native` leaf FFI calls
- Can I get a class(object) from function return value? HOT 4
- Is `Utf8` equivalent to `ffi.Char`? HOT 4
- Using @Native/@FfiNative in an FFI plugin on Android HOT 12
- Invocation native function of namespace PlatformConfigurationNativeApi HOT 2
- FFI will not run in release mode unless the Testability flag is set (IOS) HOT 3
- How can I add third party libraries ? HOT 6
- Access Uint8List as a Struct HOT 3
- App is crashing after trying to free reassigned pointer HOT 10
- The C callback function registered through ffi invalidates both asynchronous operations and timers during the callback HOT 2
- Make `Arena` and `using` availble for other abstractions than FFI? HOT 6
- FFI lookupFunction causes stack overflow HOT 2
- Merge this repo into dart-lang/native
- ffigen generating `Pointer<Int>` instead of `Array<...>` HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ffi.