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

Memory leak in interface getters #198

Closed
efreibe opened this issue Apr 4, 2021 · 3 comments · Fixed by #190
Closed

Memory leak in interface getters #198

efreibe opened this issue Apr 4, 2021 · 3 comments · Fixed by #190
Labels
bug Something isn't working

Comments

@efreibe
Copy link

efreibe commented Apr 4, 2021

I've been looking at the code trying to implement some interfaces.

I've noted that at the end of getters there is a free statement to release memory allocated earlier, but if the native function does not succeed an exception is thrown and the memory is not freed.

Let's take for example a code snipped taken from IFileOpenPicker.dart, you'll see what I mean:

  int get FileTypeFilter {
    final retValuePtr = calloc<IntPtr>();

    final hr = Pointer<NativeFunction<_get_FileTypeFilter_Native>>.fromAddress(
            ptr.ref.vtable.elementAt(14).value)
        .asFunction<_get_FileTypeFilter_Dart>()(ptr.ref.lpVtbl, retValuePtr);
    if (FAILED(hr)) throw WindowsException(hr);

    final retValue = retValuePtr.value;
    free(retValuePtr);
    return retValue;
  }

Because the pointer is generated inside the function it must be freed even if another type of exception is thrown (ie, calling .fromAddress, .asFunction, etc.)

@timsneath timsneath added the bug Something isn't working label Apr 5, 2021
@timsneath
Copy link
Contributor

Yeah, that totally needs fixing. Thanks for the heads-up.

@efreibe
Copy link
Author

efreibe commented Apr 5, 2021

I think that ffi needs to have any type of smart pointer because it's a nightmare to have to deal with freeing them at this point. Something realated to finalizers maybe?

I don't know if you have any solution for this or any workaround to avoid memory leaks before posting the issue in dart ffi.

Another issue that maybe could need to be addressed here is auto releasing interfaces (implemented in IUnknown) before garbage collection, maybe using finalizers too. I've tried to understand how they works but cannot find any working example.

@timsneath
Copy link
Contributor

We do have some samples of resource management here:
https://github.com/dart-lang/sdk/tree/master/samples/ffi/resource_management

This introduces the concept of a Pool, which can take care of releasing resources automatically when they fall out of scope. I haven't yet looked at implementing this at the library level.

@timsneath timsneath linked a pull request Apr 6, 2021 that will close this issue
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants