TM iOS: Use weak_ptr to pass around Instance to avoid unreleased refs

Summary:
There is a timing issue when reloading the bridge (in dev mode) and the tear down of the TurboModules. This causes `Instance` to never get freed, hence the "bridge" isn't cleaning up properly. The side effect can be bogus error saying that it's unable to find a module.

To address this, JSCallInvoker should just take in weak_ptr<Instance>.

Reviewed By: RSNara

Differential Revision: D14739181

fbshipit-source-id: f9f2a55486debaeb28d3d293df3cf1d3f6b9a031
This commit is contained in:
Kevin Gozali
2019-04-02 18:06:29 -07:00
committed by Facebook Github Bot
parent 2387d7d255
commit 71a8944b39
4 changed files with 8 additions and 7 deletions

View File

@@ -210,7 +210,7 @@ struct RCTInstanceCallback : public InstanceCallback {
return _jsMessageThread;
}
- (std::shared_ptr<Instance>)reactInstance
- (std::weak_ptr<Instance>)reactInstance
{
return _reactInstance;
}

View File

@@ -12,14 +12,15 @@
namespace facebook {
namespace react {
JSCallInvoker::JSCallInvoker(std::shared_ptr<Instance> reactInstance)
JSCallInvoker::JSCallInvoker(std::weak_ptr<Instance> reactInstance)
: reactInstance_(reactInstance) {}
void JSCallInvoker::invokeAsync(std::function<void()>&& func) {
if (reactInstance_ == nullptr) {
auto instance = reactInstance_.lock();
if (instance == nullptr) {
return;
}
reactInstance_->invokeAsync(std::move(func));
instance->invokeAsync(std::move(func));
}
} // namespace react

View File

@@ -26,13 +26,13 @@ class Instance;
*/
class JSCallInvoker {
public:
JSCallInvoker(std::shared_ptr<Instance> reactInstance);
JSCallInvoker(std::weak_ptr<Instance> reactInstance);
void invokeAsync(std::function<void()>&& func);
// TODO: add sync support
private:
std::shared_ptr<Instance> reactInstance_;
std::weak_ptr<Instance> reactInstance_;
};
} // namespace react

View File

@@ -91,6 +91,6 @@ private:
@interface RCTBridge ()
- (std::shared_ptr<facebook::react::MessageQueueThread>)jsMessageThread;
- (std::shared_ptr<facebook::react::Instance>)reactInstance;
- (std::weak_ptr<facebook::react::Instance>)reactInstance;
@end