fix: carousel reInit event listener not removed on unmount (memory leak)
Bug
In apps/web/src/components/ui/carousel.tsx (lines 97–106), the useEffect registers two Embla carousel event listeners but the cleanup function only removes one of them:
React.useEffect(() => {
if (!api) return
onSelect(api)
api.on("reInit", onSelect) // registered
api.on("select", onSelect) // registered
return () => {
// api?.off("reInit", onSelect) ← missing
api?.off("select", onSelect)
}
}, [api, onSelect])
The reInit listener is never unsubscribed, causing it to accumulate in memory on each mount/unmount cycle.
Fix
return () => {
api?.off("reInit", onSelect)
api?.off("select", onSelect)
}
Fix is ready on a fork branch: https://github.com/josephdscs/OpenCut/tree/fix/carousel-missing-reinit-cleanup
Fixed on my fork: https://github.com/seuthootDev/OpenCut/tree/fix/carousel-reinit-listener-leak
The cleanup function in apps/web/src/components/ui/carousel.tsx was only unregistering the select listener, so reInit stayed attached to the Embla API after unmount. Added the missing api?.off("reInit", onSelect) call.
Verified with vite build — no type/build errors introduced.
Also applies to the duplicate report in #815. Let me know if this looks good and I'll open a PR once collaborator PR access allows it.
I've resolved this on my fork:
- Branch:
fix/carousel-missing-reinit-cleanup - Commit:
5c78659
Added api?.off("reInit", onSelect) inside the useEffect cleanup function so both registered listeners are unsubscribed on unmount.