Skip to content
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

Track FFI-allocated memory? #42

Closed
timsneath opened this issue Mar 25, 2023 · 1 comment
Closed

Track FFI-allocated memory? #42

timsneath opened this issue Mar 25, 2023 · 1 comment

Comments

@timsneath
Copy link

Can this be used with FFI to track manually allocated memory (malloc / free)?

If so, it would be awesome to be able to use this to check for errors in FFI code... @dcharkes

@dcharkes
Copy link

Can this be used with FFI to track manually allocated memory (malloc / free)?

With NativeFinalizers

If you already use NativeFinalizers to invoke free: no.

Because then both the NativeFinalizer that frees the memory will run, and the Finalizer that tracks whether an object that holds on to memory cannot be canceled by the NativeFinalizer that invokes free.

Since the NativeFinalizer and leak_tracker both rely on the GC, you wouldn't get any new information anyway.

With Pointers

If you manually call free, but working directly withPointers: no.

The value and detach arguments do not count towards those objects being accessible to the program. Both must be objects supported as an Expando key. They may be the same object.

https://api.flutter.dev/flutter/dart-core/Finalizer/attach.html

Does not work on numbers, strings, booleans, records, null, dart:ffi pointers, dart:ffi structs, or dart:ffi unions.

https://api.flutter.dev/flutter/dart-core/Expando-class.html

With wrapper class without NativeFinalizers

If you call free manually, and you wrap your pointers in another class: yes.

The class that wraps pointer would have a free that cancels the Finalizer in the leak tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants