From 9cd43340a7e2443564c2ff5e8e85d37f6e1e47ef Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 3 Feb 2022 11:28:00 -0800 Subject: [PATCH] Attempt to fix crash during app termination Summary: changelog: Attempt to fix a crash during app termination on iOS in the new renderer We need to stop all surfaces before it is terminated to prevent app from crashing. The crash happens on background thread. The app crashes because it is probably accessing static variable that has been deallocated by the main thread. How can main thread deallocate things when background thread is still running? Because main thread is attached to our app's process, background thread is provided by the system and continues to live. Reviewed By: ShikaSD Differential Revision: D33977161 fbshipit-source-id: 87546d7b70d1aa465e63f0d37b70bae1fffa2304 --- React/Fabric/RCTSurfacePresenter.mm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/React/Fabric/RCTSurfacePresenter.mm b/React/Fabric/RCTSurfacePresenter.mm index 17ca2900ad392a..bab5da758a037d 100644 --- a/React/Fabric/RCTSurfacePresenter.mm +++ b/React/Fabric/RCTSurfacePresenter.mm @@ -99,6 +99,14 @@ - (instancetype)initWithContextContainer:(ContextContainer::Shared)contextContai _observers = [NSMutableArray array]; _scheduler = [self _createScheduler]; + + auto reactNativeConfig = _contextContainer->at>("ReactNativeConfig"); + if (reactNativeConfig->getBool("react_native_new_architecture:suspend_before_app_termination")) { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_applicationWillTerminate) + name:UIApplicationWillTerminateNotification + object:nil]; + } } return self; @@ -327,6 +335,11 @@ - (void)_stopAllSurfacesWithScheduler:(RCTScheduler *)scheduler }]; } +- (void)_applicationWillTerminate +{ + [self suspend]; +} + #pragma mark - RCTSchedulerDelegate - (void)schedulerDidFinishTransaction:(MountingCoordinator::Shared const &)mountingCoordinator