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

bug: Memory leak using Dart_PostCObject because need to consider the return value is true or false #6

Open
fzyzcjy opened this issue Sep 29, 2021 · 4 comments

Comments

@fzyzcjy
Copy link
Contributor

fzyzcjy commented Sep 29, 2021

Hi thanks for the lib! I suspect there is memory leak when using Dart_PostCObject. Please refer to: dart-lang/sdk#47270 (comment)

@fzyzcjy fzyzcjy changed the title bug: Memory leak when using Dart_PostCObject bug: Memory leak using Dart_PostCObject because need to consider the return value is true or false Sep 29, 2021
@shekohex
Copy link
Owner

Is this still an issue?

@fzyzcjy
Copy link
Contributor Author

fzyzcjy commented Oct 11, 2021

@shekohex Yes. allo-isolate does not handle the case when returning false. please see dart-lang/sdk#47270 (comment) and dart-lang/sdk#47270 (comment) (he mentioned he changed the code in the first post)

@shekohex
Copy link
Owner

shekohex commented Oct 12, 2021

In allo-isolate we do free the memory regardless of the Dart_PostCObject return value, this the current behavior here:
https://github.com/sunshine-protocol/allo-isolate/blob/51caf0367552bd290ce8b90c62b19db913e68c64/src/lib.rs#L119-L129

Okay, Let's take a deep dive a bit into the VM code.

Where is the Dart_PostCObject?
You will see it is defined here:
https://github.com/dart-lang/sdk/blob/a3f8a9562a8d98be7f3b1cc42bb89854f5c77c90/runtime/vm/native_api_impl.cc#L59-L61 This function does not do a lot on itself, it calls inner function here: https://github.com/dart-lang/sdk/blob/a3f8a9562a8d98be7f3b1cc42bb89854f5c77c90/runtime/vm/native_api_impl.cc#L46-L57 can only return false in one case if the message points to a null memory.

But this only happens, if the WriteApiMessage returned null which is implemented here: https://github.com/dart-lang/sdk/blob/e995cb5f7cd67d39c1ee4bdbe95c8241db36725f/runtime/vm/message_snapshot.cc#L3772-L3781

and again, this would happen if the ApiMessageSerializer failed to Serialize the message.

So, let's see how the ApiMessageSerializer handles the message!

in the Serialize method here: https://github.com/dart-lang/sdk/blob/e995cb5f7cd67d39c1ee4bdbe95c8241db36725f/runtime/vm/message_snapshot.cc#L3613-L3653 you will find it calls Push with the reference to the message https://github.com/dart-lang/sdk/blob/e995cb5f7cd67d39c1ee4bdbe95c8241db36725f/runtime/vm/message_snapshot.cc#L3066-L3071

And here is the stack_.Add(object) https://github.com/dart-lang/sdk/blob/e995cb5f7cd67d39c1ee4bdbe95c8241db36725f/runtime/vm/message_snapshot.cc#L3068 which I guess do a local copy of the value (not 100% sure yet, but I will keep dive more).

To be continued...

@fzyzcjy
Copy link
Contributor Author

fzyzcjy commented Oct 12, 2021

Thanks for the detailed reply!

To be continued...

Tell me when you finished ;) I will come back and read the rest

trueb2 added a commit to trueb2/allo-isolate that referenced this issue Apr 11, 2022
Fix ZeroCopyBuffer memory leak related to discussion in shekohex#6
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

2 participants