Skip to content

Commit a78b255

Browse files
authored
fix(šŸ›): Fix serious memory error with data loading hooks (useImage, useFont) (#2866)
Hooks like useImage wrong manually dispose the data when unmounting even thought the reference might be still used somewhere else. In the case of the Skia reconciler, the image is disposed even before any kind of unmounting is signaled to the reconciler.
1 parent fef5aba commit a78b255

File tree

4 files changed

+10
-34
lines changed

4 files changed

+10
-34
lines changed

Diff for: ā€Žapps/paper/ios/Podfile.lock

+1-7
Original file line numberDiff line numberDiff line change
@@ -1694,8 +1694,6 @@ PODS:
16941694
- ReactCommon/turbomodule/bridging
16951695
- ReactCommon/turbomodule/core
16961696
- Yoga
1697-
- RNSVG (15.9.0):
1698-
- React-Core
16991697
- SocketRocket (0.7.0)
17001698
- Yoga (0.0.0)
17011699

@@ -1770,7 +1768,6 @@ DEPENDENCIES:
17701768
- RNGestureHandler (from `../../../node_modules/react-native-gesture-handler`)
17711769
- RNReanimated (from `../../../node_modules/react-native-reanimated`)
17721770
- RNScreens (from `../../../node_modules/react-native-screens`)
1773-
- RNSVG (from `../node_modules/react-native-svg`)
17741771
- Yoga (from `../../../node_modules/react-native/ReactCommon/yoga`)
17751772

17761773
SPEC REPOS:
@@ -1915,8 +1912,6 @@ EXTERNAL SOURCES:
19151912
:path: "../../../node_modules/react-native-reanimated"
19161913
RNScreens:
19171914
:path: "../../../node_modules/react-native-screens"
1918-
RNSVG:
1919-
:path: "../node_modules/react-native-svg"
19201915
Yoga:
19211916
:path: "../../../node_modules/react-native/ReactCommon/yoga"
19221917

@@ -1989,9 +1984,8 @@ SPEC CHECKSUMS:
19891984
RNGestureHandler: 939f21fabf5d45a725c0bf175eb819dd25cf2e70
19901985
RNReanimated: 9d20a811e6987cba268ef4f56242dfabd40e85c1
19911986
RNScreens: b03d696c70cc5235ce4587fcc27ae1a93a48f98c
1992-
RNSVG: 3d2bdcaef51c8071880a9c0072fe324f4423a3ba
19931987
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
1994-
Yoga: a1d7895431387402a674fd0d1c04ec85e87909b8
1988+
Yoga: 2a45d7e59592db061217551fd3bbe2dd993817ae
19951989

19961990
PODFILE CHECKSUM: debc09f5cfcbea21f946ca0be3faa5351e907125
19971991

Diff for: ā€Žpackages/skia/src/external/reanimated/useAnimatedImageValue.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useEffect } from "react";
21
import type { FrameInfo, SharedValue } from "react-native-reanimated";
32

43
import { useAnimatedImage } from "../../skia/core/AnimatedImage";
@@ -16,14 +15,10 @@ export const useAnimatedImageValue = (
1615
const isPaused = paused ?? defaultPaused;
1716
const currentFrame = Rea.useSharedValue<null | SkImage>(null);
1817
const lastTimestamp = Rea.useSharedValue(-1);
19-
const animatedImage = useAnimatedImage(
20-
source,
21-
(err) => {
22-
console.error(err);
23-
throw new Error(`Could not load animated image - got '${err.message}'`);
24-
},
25-
false
26-
);
18+
const animatedImage = useAnimatedImage(source, (err) => {
19+
console.error(err);
20+
throw new Error(`Could not load animated image - got '${err.message}'`);
21+
});
2722
const frameDuration =
2823
animatedImage?.currentFrameDuration() || DEFAULT_FRAME_DURATION;
2924

@@ -53,11 +48,5 @@ export const useAnimatedImageValue = (
5348
// Update the last timestamp
5449
lastTimestamp.value = timestamp;
5550
});
56-
useEffect(() => {
57-
return () => {
58-
animatedImage?.dispose();
59-
};
60-
// eslint-disable-next-line react-hooks/exhaustive-deps
61-
}, []);
6251
return currentFrame;
6352
};

Diff for: ā€Žpackages/skia/src/skia/core/AnimatedImage.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ const animatedImgFactory = Skia.AnimatedImage.MakeAnimatedImageFromEncoded.bind(
1212
* */
1313
export const useAnimatedImage = (
1414
source: DataSourceParam,
15-
onError?: (err: Error) => void,
16-
managed = true
17-
) => useRawData(source, animatedImgFactory, onError, managed);
15+
onError?: (err: Error) => void
16+
) => useRawData(source, animatedImgFactory, onError);

Diff for: ā€Žpackages/skia/src/skia/core/Data.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ export const loadData = <T>(
4040

4141
const useLoading = <T extends SkJSIInstance<string>>(
4242
source: DataSourceParam,
43-
loader: () => Promise<T | null>,
44-
manage = true
43+
loader: () => Promise<T | null>
4544
) => {
4645
const mounted = useRef(false);
4746
const [data, setData] = useState<T | null>(null);
@@ -55,9 +54,6 @@ const useLoading = <T extends SkJSIInstance<string>>(
5554
}
5655
});
5756
return () => {
58-
if (manage) {
59-
dataRef.current?.dispose();
60-
}
6157
mounted.current = false;
6258
};
6359
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -84,7 +80,6 @@ export const useCollectionLoading = <T extends SkJSIInstance<string>>(
8480
});
8581

8682
return () => {
87-
dataRef.current?.forEach((instance) => instance?.dispose());
8883
mounted.current = false;
8984
};
9085

@@ -97,9 +92,8 @@ export const useCollectionLoading = <T extends SkJSIInstance<string>>(
9792
export const useRawData = <T extends SkJSIInstance<string>>(
9893
source: DataSourceParam,
9994
factory: (data: SkData) => T | null,
100-
onError?: (err: Error) => void,
101-
manage = true
102-
) => useLoading(source, () => loadData<T>(source, factory, onError), manage);
95+
onError?: (err: Error) => void
96+
) => useLoading(source, () => loadData<T>(source, factory, onError));
10397

10498
const identity = (data: SkData) => data;
10599

0 commit comments

Comments
Ā (0)