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

[ios] Fix weak objects implementation on Hermes #21986

Merged
merged 2 commits into from Apr 7, 2023

Conversation

tsapeta
Copy link
Member

@tsapeta tsapeta commented Apr 4, 2023

Why

JavaScriptWeakObject on iOS used WeakRef if it was supported by the engine (only JSC as of iOS 14.5), otherwise it worked like strong-referenced object and caused memory leaks. As of React Native 0.71, we can leverage jsi::WeakObject which is implemented on Hermes.

How

Added separate implementation for weak objects on Hermes runtimes

Test Plan

Added simple native class and created a milion of instances (on the JS side) and confirmed they are getting deallocated over time

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Apr 4, 2023
@tsapeta tsapeta marked this pull request as ready for review April 6, 2023 10:06
Comment on lines 16 to 19
Shared pointer to the `WeakRef` JS object. Available only on JSC engine.
*/
std::shared_ptr<jsi::Object> _jsObject;
std::shared_ptr<jsi::Object> _weakRef;

/**
A weak reference to a JS object. Available only on Hermes engine.
*/
std::shared_ptr<jsi::WeakObject> _weakObject;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want to have #if __has_include(<reacthermes/HermesExecutorFactory.h>) also to these member variables? and you can also use the same _weakRef name

e.g.

#if __has_include(<reacthermes/HermesExecutorFactory.h>)
  /**
   A weak reference to a JS object. Available only on Hermes engine.
   */
  std::shared_ptr<jsi::WeakObject> _weakRef;
#else
  /**
   Shared pointer to the `WeakRef` JS object. Available only on JSC engine.
   */
  std::shared_ptr<jsi::Object> _weakRef;
#endif

Copy link
Member Author

@tsapeta tsapeta Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I applied your suggestion, but used _weakObject instead of _weakRef because the latter has more sense when it refers only to WeakRef instance.

@tsapeta tsapeta force-pushed the @tsapeta/ios/weak-objects-hermes branch from 712249f to 6b6a7b2 Compare April 6, 2023 21:22
@tsapeta tsapeta force-pushed the @tsapeta/ios/weak-objects-hermes branch from 6b6a7b2 to 3b9dad7 Compare April 7, 2023 21:16
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Apr 7, 2023
@tsapeta tsapeta merged commit 2727a9f into main Apr 7, 2023
1 check passed
@tsapeta tsapeta deleted the @tsapeta/ios/weak-objects-hermes branch April 7, 2023 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants