OpenCut icon indicating copy to clipboard operation
OpenCut copied to clipboard

fix: carousel reInit event listener not removed on unmount (memory leak)

Open josephdscs opened this issue 5 months ago • 2 comments

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

josephdscs avatar Jun 09 '26 11:06 josephdscs

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.

seuthootDev avatar Aug 22 '26 10:08 seuthootDev

I've resolved this on my fork:

Added api?.off("reInit", onSelect) inside the useEffect cleanup function so both registered listeners are unsubscribed on unmount.

killer1panda avatar Sep 08 '26 08:09 killer1panda