✅ Solved: Memory leak in rive-react-native: Native Rive instances persist in memory after compone...

Native Rive instances appear to be cached/retained in memory even when:
- Components are unmounted
- `unmountOnBlur: true` is set in React Navigation
- All refs are properly cleaned up
- Components are conditionally rendered based on screen focus
1. Open app → HomeScreen (contains background Rive animation + PlantRive component)
   - Initial memory: ~750-850 MB
2. Navigate: HomeScreen → AddHabitScreen → HabitCustomizationScreen → HomeScreen
   - Memory after first cycle: ~1.3-1.5 GB
3. Repeat step 2 (2-3 more times)
   - Memory after 2-3 cycles: ~2-2.2 GB
4. App crashes due to memory pressure
- When a screen with Rive animations loses focus or unmounts, native Rive instances should be completely released from memory
- Memory usage should return to baseline (~750-850 MB) when navigating to screens without animations
- No memory accumulation across navigation cycles


Code :

// HomeScreen.tsx
import { useFocusEffect } from '@react-navigation/native';
import Rive, { Fit } from 'rive-react-native';

const HomeScreen = () => {
  const isFocused = useIsFocused();
  const riveRef = useRef<any>(null);
  const [riveKey, setRiveKey] = useState(0);

  // Cleanup on blur
  useFocusEffect(
    useCallback(() => {
      setRiveKey(prev => prev + 1); // Force remount
      
      return () => {
        try {
          if (riveRef.current) {
            riveRef.current.pause();
          }
        } catch (error) {
          console.warn('Rive cleanup error:', error);
        }
      };
    }, []),
  );

  return (
    <View>
      {/* Only render when focused */}
      {isFocused && (
        <Rive
          key={`background-rive-${riveKey}`} // Force remount
          ref={riveRef}
          resourceName="main_screen_11jul"
          autoplay={isFocused}
          fit={Fit.Cover}
          style={StyleSheet.absoluteFillObject}
        />
      )}
    </View>
  );
};


## Environment Details

- **Package**: `rive-react-native`
- **Version**: `^9.7.1`
- **React Native Version**: `0.80.0`
- **React Version**: `19.1.0`
- **Navigation**: `@react-navigation/native-stack` `^7.3.21`
- **Platform**: Both iOS and Android (issue observed on both)
- **Device**: Tested on physical devices (Android and iOS)


Image of crash message from Xcode