Permalink
Browse files

Remove usage of "this" in a callback in RCTNativeModule

Summary: As RCTNativeModule can be destructed at any time, it's unsafe to capture "this" in a callback.

Reviewed By: javache

Differential Revision: D5963728

fbshipit-source-id: c80a01c851d97813e4fead2b31c442eaeb8ae204
  • Loading branch information...
fromcelticpark authored and facebook-github-bot committed Oct 4, 2017
1 parent ef2e29f commit d07279c978994db07d229b8d19700c83d80db4fa
Showing with 14 additions and 10 deletions.
  1. +0 −1 React/CxxModule/RCTNativeModule.h
  2. +14 −9 React/CxxModule/RCTNativeModule.mm
@@ -26,7 +26,6 @@ class RCTNativeModule : public NativeModule {
private:
__weak RCTBridge *m_bridge;
RCTModuleData *m_moduleData;
MethodCallResult invokeInner(unsigned int methodId, const folly::dynamic &params);
};
}
@@ -25,6 +25,8 @@
namespace facebook {
namespace react {
static MethodCallResult invokeInner(RCTBridge *bridge, RCTModuleData *moduleData, unsigned int methodId, const folly::dynamic &params);
RCTNativeModule::RCTNativeModule(RCTBridge *bridge, RCTModuleData *moduleData)
: m_bridge(bridge)
, m_moduleData(moduleData) {}
@@ -56,16 +58,19 @@
}
void RCTNativeModule::invoke(unsigned int methodId, folly::dynamic &&params, int callId) {
// capture by weak pointer so that we can safely use these variables in a callback
__weak RCTBridge *weakBridge = m_bridge;
__weak RCTModuleData *weakModuleData = m_moduleData;
// The BatchedBridge version of this buckets all the callbacks by thread, and
// queues one block on each. This is much simpler; we'll see how it goes and
// iterate.
dispatch_block_t block = [this, methodId, params=std::move(params), callId] {
dispatch_block_t block = [weakBridge, weakModuleData, methodId, params=std::move(params), callId] {
#ifdef WITH_FBSYSTRACE
if (callId != -1) {
fbsystrace_end_async_flow(TRACE_TAG_REACT_APPS, "native", callId);
}
#endif
invokeInner(methodId, std::move(params));
invokeInner(weakBridge, weakModuleData, methodId, std::move(params));
};
dispatch_queue_t queue = m_moduleData.methodQueue;
@@ -77,23 +82,23 @@
}
MethodCallResult RCTNativeModule::callSerializableNativeHook(unsigned int reactMethodId, folly::dynamic &&params) {
return invokeInner(reactMethodId, params);
return invokeInner(m_bridge, m_moduleData, reactMethodId, params);
}
MethodCallResult RCTNativeModule::invokeInner(unsigned int methodId, const folly::dynamic &params) {
if (!m_bridge.valid) {
static MethodCallResult invokeInner(RCTBridge *bridge, RCTModuleData *moduleData, unsigned int methodId, const folly::dynamic &params) {
if (!bridge || !bridge.valid || !moduleData) {
return folly::none;
}
id<RCTBridgeMethod> method = m_moduleData.methods[methodId];
id<RCTBridgeMethod> method = moduleData.methods[methodId];
if (RCT_DEBUG && !method) {
RCTLogError(@"Unknown methodID: %ud for module: %@",
methodId, m_moduleData.name);
methodId, moduleData.name);
}
NSArray *objcParams = convertFollyDynamicToId(params);
@try {
id result = [method invokeWithBridge:m_bridge module:m_moduleData.instance arguments:objcParams];
id result = [method invokeWithBridge:bridge module:moduleData.instance arguments:objcParams];
return convertIdToFollyDynamic(result);
}
@catch (NSException *exception) {
@@ -104,7 +109,7 @@
NSString *message = [NSString stringWithFormat:
@"Exception '%@' was thrown while invoking %s on target %@ with params %@\ncallstack: %@",
exception, method.JSMethodName, m_moduleData.name, objcParams, exception.callStackSymbols];
exception, method.JSMethodName, moduleData.name, objcParams, exception.callStackSymbols];
RCTFatal(RCTErrorWithMessage(message));
}
}

0 comments on commit d07279c

Please sign in to comment.