-
Notifications
You must be signed in to change notification settings - Fork 226
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
FFI structs sample possible dangling pointer #145
Comments
Yes apparently a small oversight. You might get an error like "UTF8 unexpected byte" while running it. You can just put calloc.free after converting name to dart string. Also I think you can refer to samples folder and tests in SDK repo. There are more examples there. Samples here are relatively short. |
Hey @mit-mit can you drag in the right people to answer this question? |
Yes, this leaves a dangling pointer.
Then, this struct is returned by value, which is a pattern prone to error. In C++, one could use a C++ class instead, and have a destructor that frees the string. In C, if the desire is to pass the struct around by value, I would inline the string by value. struct Place
{
char name[64];
struct Coordinate coordinate;
};
struct Place create_place(char *name, double latitude, double longitude)
{
struct Place place;
strcpy(place.name, name);
place.coordinate = create_coordinate(latitude, longitude);
return place;
} Note that now there is a max name length. Alternatively, one could change the whole On a higher level note, this seems to be a copy and paste error from other function calls in which a |
Please ignore my ignorance but while I'm trying to learn more about
ffi
I studied the structs sample.I just don't understand how freeing
myHomeUtf8
does not leave a dangling pointer in the the nativePlace
struct.https://github.com/dart-lang/samples/blob/master/ffi/structs/structs_library/structs.c#L58
https://github.com/dart-lang/samples/blob/master/ffi/structs/structs.dart#L94
I understand that someone needs to free that memory and that in a sample memory management might not be the biggest concern. But accessing the name after freeing the memory seems especially odd to me.
Most likely I'm just missing something here.
The text was updated successfully, but these errors were encountered: