Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useFrameCallback cleanup #6143

Merged
merged 1 commit into from
Jun 20, 2024

Conversation

szydlovsky
Copy link
Contributor

Summary

useFrameCallback had a bit faulty cleanup: it used ref.current during useEffect cleanup, when the ref value could as well be unreachable (already deallocated). I added the callbackId memoization + removed unnecessary setting it to -1.

Test plan

useFrameCallback example from Example App, as well as this code:

Code
import React from 'react';
import { StyleSheet, View } from 'react-native';
import Animated, {
  useFrameCallback,
  useSharedValue,
  useAnimatedStyle,
} from 'react-native-reanimated';

export default function App() {
  const t = useSharedValue<number>(0);
  const height = 1300;

  useFrameCallback((frameInfo) => {
    t.value = frameInfo.timeSinceFirstFrame / 350;
  });

  const infinityStyle = useAnimatedStyle(() => {
    const scale = 2 / (3 - Math.cos(2 * t.value));
    return {
      transform: [
        { translateX: scale * (Math.sin(2 * t.value) / 2) * 200 },
        {
          translateY:
            scale * Math.cos(t.value) * Math.min(height / 2 - 120, 200),
        },
      ],
    };
  });

  return (
    <View style={styles.container}>
      <Animated.View style={[styles.dot, infinityStyle]} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    height: 150,
  },
  dot: {
    width: 60,
    height: 60,
    borderRadius: 30,
    backgroundColor: '#b58df1',
    position: 'absolute',
  },
});

@szydlovsky szydlovsky requested a review from piaskowyk June 20, 2024 10:26
Copy link
Member

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but let's wait for @piaskowyk's approval

@szydlovsky szydlovsky added this pull request to the merge queue Jun 20, 2024
Merged via the queue into main with commit 545ed7f Jun 20, 2024
7 checks passed
@szydlovsky szydlovsky deleted the @szydlovsky/useFrameCallback-cleanup-fix branch June 20, 2024 13:49
@andreialecu
Copy link
Contributor

Not sure if this is the cause, but we started running into #6299 recently.

I made a fix in #6301

github-merge-queue bot pushed a commit that referenced this pull request Jul 24, 2024
## Summary

#6143
was supposed to improve cleanup of `useFrameCallback` but also deleted a
needed line - setting `callbackId` of callback registry to -1. Seems
like some code is dependent on this value and it caused some crashes
such as this one:
#6143. I
haven't found a reliable way to reproduce the issue since we got the
info from issue creator's sentry. Nonetheless, analysis of the code has
shown that we need to set the `callbackId` to -1 on cleanup, otherwise
we perform operations that shouldn't be done anymore (e.g there's this
check:
https://github.com/software-mansion/react-native-reanimated/blob/818ed78870bf41b6255abdd16b153444761101b2/packages/react-native-reanimated/src/frameCallback/FrameCallbackRegistryUI.ts#L105-L107).

## Test plan

As stated, the issue isn't really reproducible but the change doesn't
risk any regression and time will tell if it fixes the problem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants