From f2d65ea85be5aa0d7bdf87a639d869afc18245d1 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Mon, 20 Jul 2015 02:34:11 -0700 Subject: [PATCH] [ReactNative] Fix RCTJavaScriptContext deallocation (attempt #2) 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. --- React/Base/RCTBatchedBridge.m | 67 +++++++++++----------------- React/Executors/RCTContextExecutor.m | 1 + 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 98fdb5115e8b2d..7975b5e7cbbc25 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -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)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)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 diff --git a/React/Executors/RCTContextExecutor.m b/React/Executors/RCTContextExecutor.m index 5fc5296459e2ce..173179c5ac23bf 100644 --- a/React/Executors/RCTContextExecutor.m +++ b/React/Executors/RCTContextExecutor.m @@ -320,6 +320,7 @@ - (void)invalidate onThread:_javaScriptThread withObject:nil waitUntilDone:NO]; + _context = nil; } - (void)dealloc