Skip to content

Commit e7bd0f0

Browse files
fromcelticparkfacebook-github-bot
authored andcommittedNov 23, 2017
Remove self capturing RCTCxxBridge->_pendingCalls
Differential Revision: D6387237 fbshipit-source-id: 3244bba439ba9fc38c5be09657cbdc787b9b4585
1 parent 7d969a0 commit e7bd0f0

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed
 

‎React/CxxBridge/RCTCxxBridge.mm

+25-13
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ @interface RCTCxxBridge : RCTBridge
6161

6262
static NSString *const RCTJSThreadName = @"com.facebook.react.JavaScript";
6363

64+
typedef void (^RCTPendingCall)();
65+
6466
using namespace facebook::react;
6567

6668
/**
@@ -157,7 +159,7 @@ @implementation RCTCxxBridge
157159
BOOL _wasBatchActive;
158160
BOOL _didInvalidate;
159161

160-
NSMutableArray<dispatch_block_t> *_pendingCalls;
162+
NSMutableArray<RCTPendingCall> *_pendingCalls;
161163
std::atomic<NSInteger> _pendingCount;
162164

163165
// Native modules
@@ -972,7 +974,7 @@ - (void)logMessage:(NSString *)message level:(NSString *)level
972974

973975
#pragma mark - RCTBridge methods
974976

975-
- (void)_runAfterLoad:(dispatch_block_t)block
977+
- (void)_runAfterLoad:(RCTPendingCall)block
976978
{
977979
// Ordering here is tricky. Ideally, the C++ bridge would provide
978980
// functionality to defer calls until after the app is loaded. Until that
@@ -1025,9 +1027,9 @@ - (void)_flushPendingCalls
10251027
// Phase B: _flushPendingCalls happens. Each block in _pendingCalls is
10261028
// executed, adding work to the queue, and _pendingCount is decremented.
10271029
// loading is set to NO.
1028-
NSArray *pendingCalls = _pendingCalls;
1030+
NSArray<RCTPendingCall> *pendingCalls = _pendingCalls;
10291031
_pendingCalls = nil;
1030-
for (dispatch_block_t call in pendingCalls) {
1032+
for (RCTPendingCall call in pendingCalls) {
10311033
call();
10321034
_pendingCount--;
10331035
}
@@ -1050,18 +1052,23 @@ - (void)enqueueJSCall:(NSString *)module method:(NSString *)method args:(NSArray
10501052
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTCxxBridge enqueueJSCall:]", nil);
10511053

10521054
RCTProfileBeginFlowEvent();
1053-
[self _runAfterLoad:^{
1055+
__weak __typeof(self) weakSelf = self;
1056+
[self _runAfterLoad:^(){
10541057
RCTProfileEndFlowEvent();
1058+
__strong __typeof(weakSelf) strongSelf = weakSelf;
1059+
if (!strongSelf) {
1060+
return;
1061+
}
10551062

1056-
if (self->_reactInstance) {
1057-
self->_reactInstance->callJSFunction([module UTF8String], [method UTF8String],
1058-
convertIdToFollyDynamic(args ?: @[]));
1063+
if (strongSelf->_reactInstance) {
1064+
strongSelf->_reactInstance->callJSFunction([module UTF8String], [method UTF8String],
1065+
convertIdToFollyDynamic(args ?: @[]));
10591066

10601067
// ensureOnJavaScriptThread may execute immediately, so use jsMessageThread, to make sure
10611068
// the block is invoked after callJSFunction
10621069
if (completion) {
1063-
if (self->_jsMessageThread) {
1064-
self->_jsMessageThread->runOnQueue(completion);
1070+
if (strongSelf->_jsMessageThread) {
1071+
strongSelf->_jsMessageThread->runOnQueue(completion);
10651072
} else {
10661073
RCTLogWarn(@"Can't invoke completion without messageThread");
10671074
}
@@ -1086,11 +1093,16 @@ - (void)enqueueCallback:(NSNumber *)cbID args:(NSArray *)args
10861093
*/
10871094

10881095
RCTProfileBeginFlowEvent();
1089-
[self _runAfterLoad:^{
1096+
__weak __typeof(self) weakSelf = self;
1097+
[self _runAfterLoad:^(){
10901098
RCTProfileEndFlowEvent();
1099+
__strong __typeof(weakSelf) strongSelf = weakSelf;
1100+
if (!strongSelf) {
1101+
return;
1102+
}
10911103

1092-
if (self->_reactInstance) {
1093-
self->_reactInstance->callJSCallback([cbID unsignedLongLongValue], convertIdToFollyDynamic(args ?: @[]));
1104+
if (strongSelf->_reactInstance) {
1105+
strongSelf->_reactInstance->callJSCallback([cbID unsignedLongLongValue], convertIdToFollyDynamic(args ?: @[]));
10941106
}
10951107
}];
10961108
}

0 commit comments

Comments
 (0)
Please sign in to comment.