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

[Discussion] Should we free TEXT? #178

Closed
Sunbreak opened this issue Mar 13, 2021 · 4 comments
Closed

[Discussion] Should we free TEXT? #178

Sunbreak opened this issue Mar 13, 2021 · 4 comments

Comments

@Sunbreak
Copy link
Contributor

https://github.com/timsneath/win32/blob/cc4870102e95b0c2cf0f4ca31e15baa1f783fe9e/lib/src/utils.dart#L22

TEXT uses toNativeUtf16, which uses allocator

https://github.com/dart-lang/ffi/blob/24eb0a066d3b32e54682a57e4780af4e2efbd2d3/lib/src/utf16.dart#L76-L83

  Pointer<Utf16> toNativeUtf16({Allocator allocator = malloc}) {
    final units = codeUnits;
    final Pointer<Uint16> result = allocator<Uint16>(units.length + 1);
    final Uint16List nativeString = result.asTypedList(units.length + 1);
    nativeString.setRange(0, units.length, units);
    nativeString[units.length] = 0;
    return result.cast();
  }

But code in samples doesn't take care of free

https://github.com/timsneath/win32/blob/cc4870102e95b0c2cf0f4ca31e15baa1f783fe9e/example/hello.dart#L41

Related: dart-archive/ffi#86

@timsneath
Copy link
Contributor

Yes, you absolutely should. Sorry -- some of my samples are very shoddy in this regard, since they're short lived and the process will free up the memory on exit. I've gradually moved some of them over to use a try... finally block, but I should fix up the rest.

@timsneath timsneath added external help wanted Looking for help on this ticket from the community quality and removed external help wanted Looking for help on this ticket from the community labels Mar 29, 2021
@timsneath
Copy link
Contributor

I've made a number of fixes to this in the short term. As you say, it's important to do this in production code, but it makes some of the samples rather painful to read because of all the allocation / deallocation. Ultimately I'd like to fix this by using the Pool feature so that these are automatically deallocated when they fall out of scope.

@Sunbreak
Copy link
Contributor Author

BTW, should we free Pointer<COMObject> in object from CoCreateInstance ?

https://github.com/timsneath/win32/blob/a608e2e47c8c6f2cc94180b4de101592b308a54c/example/wmi.dart#L43-L57

if (FAILED(hr)) {
  // ...
  free(pLoc.ptr);
  CoUninitialize();
  throw exception; 
}

@timsneath
Copy link
Contributor

I think I've fixed all instances of this; please reopen if you find any further leaks.

@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants