Skip to content

Commit e79920c

Browse files
andrewdacenkometa-codesync[bot]
authored andcommitted
Fix EXC_BAD_ACCESS for animated (facebook#54158)
Summary: Pull Request resolved: facebook#54158 Changelog: [Internal] Animated was capturing `self` directly in C++ std::function contexts. When capturing Objective-C objects in C++ lambdas, they aren't automatically retained. When these lambdas were invoked later (potentially after `self` was deallocated), accessing `_onRender` and other instance variables caused the crash. Reviewed By: zeyap Differential Revision: D84638972 fbshipit-source-id: 38f8630cf4f677e29afff07348c2d5dbb1f5cd08
1 parent 094794a commit e79920c

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,32 @@ - (void)_onDisplayLinkTick
3838
{
3939
if (facebook::react::ReactNativeFeatureFlags::cxxNativeAnimatedEnabled()) {
4040
if (name == facebook::react::AnimatedModule::kModuleName) {
41+
__weak RCTAnimatedModuleProvider *weakSelf = self;
4142
auto provider = std::make_shared<facebook::react::NativeAnimatedNodesManagerProvider>(
42-
[self](std::function<void()> &&onRender) {
43-
_onRender = onRender;
44-
if (_displayLink == nil) {
43+
[weakSelf](std::function<void()> &&onRender) {
44+
RCTAnimatedModuleProvider *strongSelf = weakSelf;
45+
if (strongSelf) {
46+
strongSelf->_onRender = onRender;
47+
if (strongSelf->_displayLink == nil) {
4548
#if TARGET_OS_OSX
46-
_displayLink = [RCTPlatformDisplayLink displayLinkWithTarget:self selector:@selector(_onDisplayLinkTick)];
49+
strongSelf->_displayLink = [RCTPlatformDisplayLink displayLinkWithTarget:strongSelf
50+
selector:@selector(_onDisplayLinkTick)];
4751
#else
48-
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_onDisplayLinkTick)];
52+
strongSelf->_displayLink = [CADisplayLink displayLinkWithTarget:strongSelf
53+
selector:@selector(_onDisplayLinkTick)];
4954
#endif
50-
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
55+
[strongSelf->_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
56+
}
5157
}
5258
},
53-
[self]() {
54-
if (_displayLink != nil) {
55-
[_displayLink invalidate];
56-
_displayLink = nil;
57-
_onRender = nullptr;
59+
[weakSelf]() {
60+
RCTAnimatedModuleProvider *strongSelf = weakSelf;
61+
if (strongSelf) {
62+
if (strongSelf->_displayLink != nil) {
63+
[strongSelf->_displayLink invalidate];
64+
strongSelf->_displayLink = nil;
65+
strongSelf->_onRender = nullptr;
66+
}
5867
}
5968
});
6069
return std::make_shared<facebook::react::AnimatedModule>(std::move(jsInvoker), std::move(provider));

0 commit comments

Comments
 (0)