Commit 8403694
authored
Fix memory leak with mixed use of
## Description
If someone created a non-`useMemo` composedGesture with two gestures one
with, and one without `useMemo`, the `config.simultanousWith` array
would had held a stale reference to a gesture from a previous render,
thus producing a memory leak.
## FIx
We hold `GestureRefs` instead of only `handlerTags` because when
initializing the array `handlerTags` are not set, we can simply collapse
the array to store only `handlerTags` and remove duplicates before
passing them down to native.
## Test plan
Tested on the following code.
```tsx
import { useMemo, useState } from "react";
import {
Gesture,
GestureDetector,
GestureHandlerRootView,
RectButton,
} from "react-native-gesture-handler";
import { View } from "react-native";
const Leak = () => {
const [count, setCount] = useState(0);
const bigMemory = new Array(10000);
const pinchGesture = useMemo(() => Gesture.Pinch(), []);
const trackGesture = Gesture.Pan().onUpdate(() => {
console.log("trackGesture", bigMemory.length);
});
const gestures = Gesture.Simultaneous(pinchGesture, trackGesture);
return (
<GestureHandlerRootView style={{ flex: 1, backgroundColor: "#f4f4f4" }}>
<GestureDetector gesture={gestures}>
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<RectButton
onPress={() => setCount(c => c + 1)}
style={{
backgroundColor: "#007AFF",
paddingVertical: 14,
paddingHorizontal: 28,
borderRadius: 10,
shadowColor: "#000",
shadowOpacity: 0.15,
shadowOffset: { width: 0, height: 3 },
shadowRadius: 4,
elevation: 4,
}}
>
</RectButton>
</View>
</GestureDetector>
</GestureHandlerRootView>
);
};
export default Leak;
```useMemo (#3763)1 parent ff83c88 commit 8403694
1 file changed
Lines changed: 14 additions & 8 deletions
Lines changed: 14 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
58 | | - | |
59 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
60 | 64 | | |
61 | 65 | | |
62 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
63 | 69 | | |
64 | 70 | | |
65 | | - | |
66 | | - | |
67 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
68 | 74 | | |
69 | 75 | | |
70 | 76 | | |
| |||
0 commit comments