dart-lang / dart-lang/samples

FFI structs sample possible dangling pointer

Open
#145 3 comments 0 reactions 1 assignee Assigned to @dcharkes View on GitHub
area-ffi
Dominant language
Dart
Stars
656
Forks
228
Avg merge
19h 14m
Merged PRs (30d)
4

Description

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 native `Place` struct.

https://github.com/dart-lang/samples/blob/master/ffi/structs/structs_library/structs.c#L58
```c
struct Place create_place(char *name, double latitude, double longitude)
{
struct Place place;
place.name = name;
// ...
```

https://github.com/dart-lang/samples/blob/master/ffi/structs/structs.dart#L94
```dart
final myHomeUtf8 = 'My Home'.toNativeUtf8();
final createPlace =
dylib.lookupFunction('create_place');
final place = createPlace(myHomeUtf8, 42.0, 24.0);
calloc.free(myHomeUtf8);
final name = place.name.toDartString();
```

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.