Skip to content

Commit a38496a

Browse files
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
1 parent 510cc0c commit a38496a

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ - (void)_setStateAndResubscribeImageResponseObserver:(const ImageShadowNode::Con
140140
observerCoordinator.removeObserver(_imageResponseObserverProxy);
141141
}
142142

143+
// Release the previous image to prevent memory accumulation
144+
// when the source prop changes rapidly (e.g., in animations/slideshows).
145+
// The UIImageView retains the UIImage, preventing deallocation until
146+
// the next image arrives, which can cause significant memory growth.
147+
_imageView.image = nil;
148+
143149
_state = state;
144150

145151
if (_state) {
@@ -301,4 +307,4 @@ - (void)didReceiveFailure:(NSError *)error fromObserver:(const void *)observer
301307
Class<RCTComponentViewProtocol> RCTImageCls(void)
302308
{
303309
return RCTImageComponentView.class;
304-
}
310+
}

0 commit comments

Comments
 (0)