Skip to content

Commit 7d07ca5

Browse files
committed
fix: guard against use-after-dispose in effect cleanups
When deps change, useDisposableMemo disposes the old property during render, but the useEffect cleanup for that property runs later. Wrap removeListener/removeListeners calls in try-catch since native dispose() already handles listener cleanup. Also protect render-phase cleanup in useDisposableMemo against throws.
1 parent d22b4c4 commit 7d07ca5

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/hooks/useDisposableMemo.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ export function useDisposableMemo<T>(
7070
ref.current.pendingDisposal = null;
7171
}
7272
if (ref.current.deps !== UNINITIALIZED) {
73-
cleanupRef.current(ref.current.value);
73+
try {
74+
cleanupRef.current(ref.current.value);
75+
} catch {
76+
// Swallow cleanup errors — the old value is being replaced regardless.
77+
}
7478
}
7579
ref.current = { value: factory(), deps, pendingDisposal: null };
7680
}

src/hooks/useRiveList.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ export function useRiveList(
4848
});
4949

5050
return () => {
51-
removeListener();
52-
property.removeListeners();
51+
try {
52+
removeListener();
53+
property.removeListeners();
54+
} catch {
55+
// Property may already be disposed by useDisposableMemo (deps change).
56+
// Native dispose() handles listener cleanup, so this is safe to ignore.
57+
}
5358
};
5459
}, [property]);
5560

src/hooks/useRiveProperty.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ export function useRiveProperty<P extends ViewModelProperty, T>(
8686
});
8787

8888
return () => {
89-
removeListener();
89+
try {
90+
removeListener();
91+
} catch {
92+
// Property may already be disposed by useDisposableMemo (deps change).
93+
// Native dispose() handles listener cleanup, so this is safe to ignore.
94+
}
9095
};
9196
}, [options, property]);
9297

0 commit comments

Comments
 (0)