Skip to content

Commit

Permalink
[ReactNative] Fix RCTJavaScriptContext deallocation (attempt facebook#2)
Browse files Browse the repository at this point in the history
Summary:
The context wasn't being explicitly released before, since it'd be immediately
released. Now that the executors are bridge modules, it was only being deallocated
when the modules were released, what caused the threads to not be released at all.
  • Loading branch information
tadeuzagallo committed Jul 20, 2015
1 parent 4a262e0 commit f2d65ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
67 changes: 25 additions & 42 deletions React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -344,53 +344,36 @@ - (void)invalidate
RCTLatestExecutor = nil;
}

void (^mainThreadInvalidate)(void) = ^{
RCTAssertMainThread();

[_mainDisplayLink invalidate];
_mainDisplayLink = nil;

// Invalidate modules
dispatch_group_t group = dispatch_group_create();
for (RCTModuleData *moduleData in _modules) {
if ([moduleData.instance respondsToSelector:@selector(invalidate)]) {
[moduleData dispatchBlock:^{
[(id<RCTInvalidating>)moduleData.instance invalidate];
} dispatchGroup:group];
}
moduleData.queue = nil;
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
_modules = nil;
_modulesByName = nil;
_frameUpdateObservers = nil;
});
};
[_mainDisplayLink invalidate];
_mainDisplayLink = nil;

if (!_javaScriptExecutor) {
// Invalidate modules
dispatch_group_t group = dispatch_group_create();
for (RCTModuleData *moduleData in _modules) {
if (moduleData.instance == _javaScriptExecutor) {
continue;
}

// No JS thread running
mainThreadInvalidate();
return;
if ([moduleData.instance respondsToSelector:@selector(invalidate)]) {
[moduleData dispatchBlock:^{
[(id<RCTInvalidating>)moduleData.instance invalidate];
} dispatchGroup:group];
}
moduleData.queue = nil;
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[_javaScriptExecutor executeBlockOnJavaScriptQueue:^{
[_jsDisplayLink invalidate];
_jsDisplayLink = nil;

[_javaScriptExecutor executeBlockOnJavaScriptQueue:^{

/**
* JS Thread deallocations
*/
[_javaScriptExecutor invalidate];
_javaScriptExecutor = nil;

[_jsDisplayLink invalidate];
_jsDisplayLink = nil;

/**
* Main Thread deallocations
*/
dispatch_async(dispatch_get_main_queue(), mainThreadInvalidate);
[_javaScriptExecutor invalidate];
_javaScriptExecutor = nil;
}];

}];
_modules = nil;
_modulesByName = nil;
_frameUpdateObservers = nil;
});
}

#pragma mark - RCTBridge methods
Expand Down
1 change: 1 addition & 0 deletions React/Executors/RCTContextExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ - (void)invalidate
onThread:_javaScriptThread
withObject:nil
waitUntilDone:NO];
_context = nil;
}

- (void)dealloc
Expand Down

0 comments on commit f2d65ea

Please sign in to comment.