From a38496af370bff907408d9cebf78a266195eeb9a Mon Sep 17 00:00:00 2001 From: Muhammad Suleman <53903082+MuhammadSuleman97@users.noreply.github.com> Date: Sun, 7 Jun 2026 00:09:02 +1000 Subject: [PATCH] fix(iOS): release previous UIImage on source prop change to prevent memory leak When the Image component's source prop changes, the UIImageView retains the previous UIImage until the new image loads. On rapid source changes (e.g., image carousels, animations), this causes memory accumulation since old images are not released promptly. Explicitly setting _imageView.image = nil in _setStateAndResubscribeImageResponseObserver: ensures the previous UIImage is released when the state (and thus source) changes, preventing unbounded memory growth. Fixes #12220 --- .../ComponentViews/Image/RCTImageComponentView.mm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm index 8f60235ca3b6..1242964f6283 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm @@ -140,6 +140,12 @@ - (void)_setStateAndResubscribeImageResponseObserver:(const ImageShadowNode::Con observerCoordinator.removeObserver(_imageResponseObserverProxy); } + // Release the previous image to prevent memory accumulation + // when the source prop changes rapidly (e.g., in animations/slideshows). + // The UIImageView retains the UIImage, preventing deallocation until + // the next image arrives, which can cause significant memory growth. + _imageView.image = nil; + _state = state; if (_state) { @@ -301,4 +307,4 @@ - (void)didReceiveFailure:(NSError *)error fromObserver:(const void *)observer Class RCTImageCls(void) { return RCTImageComponentView.class; -} +} \ No newline at end of file