diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index cea1e0d840d..163fe44fd1d 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -210,7 +210,7 @@ struct RCTInstanceCallback : public InstanceCallback { return _jsMessageThread; } -- (std::shared_ptr)reactInstance +- (std::weak_ptr)reactInstance { return _reactInstance; } diff --git a/ReactCommon/turbomodule/core/JSCallInvoker.cpp b/ReactCommon/turbomodule/core/JSCallInvoker.cpp index 848e9de9018..f58adb91f30 100644 --- a/ReactCommon/turbomodule/core/JSCallInvoker.cpp +++ b/ReactCommon/turbomodule/core/JSCallInvoker.cpp @@ -12,14 +12,15 @@ namespace facebook { namespace react { -JSCallInvoker::JSCallInvoker(std::shared_ptr reactInstance) +JSCallInvoker::JSCallInvoker(std::weak_ptr reactInstance) : reactInstance_(reactInstance) {} void JSCallInvoker::invokeAsync(std::function&& func) { - if (reactInstance_ == nullptr) { + auto instance = reactInstance_.lock(); + if (instance == nullptr) { return; } - reactInstance_->invokeAsync(std::move(func)); + instance->invokeAsync(std::move(func)); } } // namespace react diff --git a/ReactCommon/turbomodule/core/JSCallInvoker.h b/ReactCommon/turbomodule/core/JSCallInvoker.h index eff18fa878d..770cdf2545d 100644 --- a/ReactCommon/turbomodule/core/JSCallInvoker.h +++ b/ReactCommon/turbomodule/core/JSCallInvoker.h @@ -26,13 +26,13 @@ class Instance; */ class JSCallInvoker { public: - JSCallInvoker(std::shared_ptr reactInstance); + JSCallInvoker(std::weak_ptr reactInstance); void invokeAsync(std::function&& func); // TODO: add sync support private: - std::shared_ptr reactInstance_; + std::weak_ptr reactInstance_; }; } // namespace react diff --git a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h index d63d6922268..dbd31c8f4df 100644 --- a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h +++ b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h @@ -91,6 +91,6 @@ private: @interface RCTBridge () - (std::shared_ptr)jsMessageThread; -- (std::shared_ptr)reactInstance; +- (std::weak_ptr)reactInstance; @end