-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[dart:ffi] Default initial struct values are uninitialised on allocation #43596
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
Comments
cc @dcharkes |
Actually, the memory is uninitialized. So, I would expect a final test = allocate<Test>().ref;
print(test.test);
print(test.test == null);
Which is exactly what I see locally (MacOS). Can you give more details on how to reproduce this? Is this the full Dart program? How are you running it (asserts are disabled by default)? What platform? |
So I'm writing a flutter embedder in dart and I created the bindings using |
The equivalent of what you're doing in Dart ( MyStruct* ptr = malloc(sizeof(MyStruct)); // Allocates memory, does not initialise.
MyStruct test = *ptr; // Uses uninitialised memory. Which is not the same as what you'd be normally doing in C: MyStruct test; When adding support for passing struct by value(#36730), we're adding support for structs backed by TypedData instead of Pointer. TypedData is initialised to 0 and automatically garbage collected. For now, you should manually initialise the backing memory. (You could open an issue on the ffigen repository to provide a static method on all generated Structs that initialises the backing memory, and use those instead of |
@jld3103 As explained above, allocating memory using Closing this issue since this is working as intended. |
When allocating a struct the default value for all pointer fields is
null
, but that is really counter intuitive, because in they should benullptr
like in C/C++.This code demonstrates it:
The text was updated successfully, but these errors were encountered: