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

Fix memory mismanagement bug with EventAttachInfo #268

Merged
merged 4 commits into from
Feb 2, 2024

Conversation

MaxBen93
Copy link
Contributor

@MaxBen93 MaxBen93 commented Feb 2, 2024

Why is this change being made?

We detected a latent issue (#267) impacting the reliability of our application(s) using react-native-xaml which appeared to be the result of memory being used after it had previously been freed/released. Although this ocurred most often when being run under Application Verifier, it stands to reason that this issue could potentially be reproduced on any machine at any time.

What is being changed?

There were two main issues here:

  1. The EventAttachInfo struct is being updated to hold a property ref-counted std::shared_ptr<XamlMetadata> instead of a reference to a XamlMetadata object

    In effect, the previous implementation meant that the EventAttachInfo was holding on to a pointer to the EventAttachInfo object, completely irrespective of the fact that it was extending std::enable_shared_from_this, so it wouldn't participate in ref-counting. This would allow the object to be destroyed even if we believed there was still a strong reference to it.

  2. The EventAttachInfo struct is now passed by-value into the DispatchTheEvent and code-gen'd attachHandlers_t functions, instead of by-reference.

    Since the code-gen'd attachHandlders_t lambdas would basically just capture the EventAttachInfo inside another, nested lambda (which was provided as the callback handler for the XAML control's event), passing by-reference was a dangerous pattern to use. Although this may all happen on the same thread, we know that the XAML event callbacks could happen at any point, much later in the future. So, we can't assume that the original reference to the EventAttachInfo captured in the lambda is actually still valid memory.

While, in theory, we could use std::weak_ptr within the EventAttachInfo as perhaps a more direct replacement for how the EventAttachInfo& was previously being used, the minimal extra overhead of using a strong reference seems to justify reducing the risk of leaving open other latent issues or introducing new issues.

Microsoft Reviewers: Open in CodeFlow

@MaxBen93 MaxBen93 requested a review from a team as a code owner February 2, 2024 17:32
… XamlMetadata, as well as removes dangerous pass-by-reference instances when dealing with async code.
@jonthysell
Copy link
Contributor

Resolves #267

@jonthysell
Copy link
Contributor

Waiting for #270 to get in, then this PR should be able to pass.

@jonthysell jonthysell enabled auto-merge (squash) February 2, 2024 23:10
@jonthysell jonthysell merged commit ce90303 into microsoft:main Feb 2, 2024
16 checks passed
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

Successfully merging this pull request may close these issues.

2 participants