Skip to content

Commit 71a8944

Browse files
fkgozalifacebook-github-bot
authored andcommitted
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
1 parent 2387d7d commit 71a8944

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

React/CxxBridge/RCTCxxBridge.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ - (void) setRCTTurboModuleLookupDelegate:(id<RCTTurboModuleLookupDelegate>)turbo
210210
return _jsMessageThread;
211211
}
212212

213-
- (std::shared_ptr<Instance>)reactInstance
213+
- (std::weak_ptr<Instance>)reactInstance
214214
{
215215
return _reactInstance;
216216
}

ReactCommon/turbomodule/core/JSCallInvoker.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
namespace facebook {
1313
namespace react {
1414

15-
JSCallInvoker::JSCallInvoker(std::shared_ptr<Instance> reactInstance)
15+
JSCallInvoker::JSCallInvoker(std::weak_ptr<Instance> reactInstance)
1616
: reactInstance_(reactInstance) {}
1717

1818
void JSCallInvoker::invokeAsync(std::function<void()>&& func) {
19-
if (reactInstance_ == nullptr) {
19+
auto instance = reactInstance_.lock();
20+
if (instance == nullptr) {
2021
return;
2122
}
22-
reactInstance_->invokeAsync(std::move(func));
23+
instance->invokeAsync(std::move(func));
2324
}
2425

2526
} // namespace react

ReactCommon/turbomodule/core/JSCallInvoker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class Instance;
2626
*/
2727
class JSCallInvoker {
2828
public:
29-
JSCallInvoker(std::shared_ptr<Instance> reactInstance);
29+
JSCallInvoker(std::weak_ptr<Instance> reactInstance);
3030

3131
void invokeAsync(std::function<void()>&& func);
3232
// TODO: add sync support
3333

3434
private:
35-
std::shared_ptr<Instance> reactInstance_;
35+
std::weak_ptr<Instance> reactInstance_;
3636
};
3737

3838
} // namespace react

ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule {
9191
@interface RCTBridge ()
9292

9393
- (std::shared_ptr<facebook::react::MessageQueueThread>)jsMessageThread;
94-
- (std::shared_ptr<facebook::react::Instance>)reactInstance;
94+
- (std::weak_ptr<facebook::react::Instance>)reactInstance;
9595

9696
@end

0 commit comments

Comments
 (0)