diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 193e97448dea98..1e44149682d218 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -29,6 +29,7 @@ @implementation RCTDeviceInfo { NSDictionary *_currentInterfaceDimensions; BOOL _isFullscreen; std::atomic _invalidated; + __weak UIWindow *_keyWindow; } static NSString *const kFrameKeyPath = @"frame"; @@ -40,7 +41,8 @@ @implementation RCTDeviceInfo { - (instancetype)init { if (self = [super init]) { - [RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; + _keyWindow = RCTKeyWindow(); + [_keyWindow addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; } return self; } @@ -131,7 +133,7 @@ - (void)_cleanupObservers [[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil]; - [RCTKeyWindow() removeObserver:self forKeyPath:kFrameKeyPath]; + [_keyWindow removeObserver:self forKeyPath:kFrameKeyPath]; #if TARGET_OS_IOS [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];