-
Notifications
You must be signed in to change notification settings - Fork 231
Memory leak in dart:ffi samples #93
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
The snippet actually contains two memory leaks, samples/ffi/structs/structs.dart Lines 65 to 69 in 21e809c
should be final reverse = dylib.lookupFunction<reverse_native, Reverse>('reverse');
final backwards = 'backwards'.toNativeUtf8();
final reversedMessage =
reverse(backwards, backwards.length).toDartString();
malloc.free(backwards);
print('$backwards reversed is $reversedMessage');
malloc.free(reversedMessage); However, this is slightly ugly, because now Dart is calling It would be cleaner to add a |
Thanks. I think it would make sense to cover this in the sample code since memory management is a common issue with ffi code. |
Here the memory is allocated in
reverse()
:samples/ffi/structs/structs_library/structs.c
Line 31 in 21e809c
Here it used in
toDartString()
:samples/ffi/structs/structs.dart
Lines 67 to 68 in 21e809c
It is not clear how the allocated memory should be freed.
Should it be done using
malloc.free()
call or there is a more conventional way to do it in dart? OTOH, iftoDartString()
actually takes ownership for this C string and there is no memory leak, then this behavior should be documented somewhere.The text was updated successfully, but these errors were encountered: