Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Finalizing typed list to free its native memory #190

Closed
DanielSWolf opened this issue Apr 18, 2023 · 2 comments
Closed

Finalizing typed list to free its native memory #190

DanielSWolf opened this issue Apr 18, 2023 · 2 comments

Comments

@DanielSWolf
Copy link

I have a number of native libraries for sound processing that I want to expose as safe Dart wrappers. Each of these C functions takes one or more pointers to memory buffers, then performs some operation reading from or writing to these buffers. Often, a buffer used to get the output of one function can then be used as input to the next, preventing unnecessary copies.

My problem is how to represent these buffers on the Dart side. In #31, it was recommended to allocate memory on the native side, then use Pointer.asTypedList() to create the Dart counterpart. However, unless these buffers are static, I'll need to free them at some point. Ideally, this should happen automatically when the typed list on the Dart side is CG'ed. But the result of asTypedList is just a Float32List, which doesn't implement Finalizable, so I don't see a way of attaching a finalizer.

Ideally, I'd want one of the following:

  1. asTypedList returns a value that implements Finalizable. This way, I can attach a native finalizer to it that frees the native memory.
  2. dart:ffi offers a way to create a ByteBuffer that manages its own memory and exposes a native pointer to that memory.

Without support from dart:ffi, the best I can think of is to write a new class that implements List<double> and wraps a Float32List along with the code for automatically freeing the native memory on finalization. But that would introduce even more indirection when accessing its items from Dart, probably eliminating the performance benefits gained from not copying the buffers.

@dcharkes
Copy link
Contributor

Instead of asTypedList returning an implements Finalizable, we would could add a finalizer argument to that call.

XyzList Pointer<Xyz>.asTypedList(int length, {NativeFinalizerFunction finalizer})

This is being tracked in:

@DanielSWolf
Copy link
Author

I didn't find the existing ticket because I was looking in the wrong repo. Thanks for the pointer!

Closing this as a duplicate.

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

No branches or pull requests

2 participants