Skip to content

cleanup rive on unmount#366

Merged
bodymovin merged 1 commit into
mainfrom
NNNN-fix_leak_on_unmount
Jun 18, 2025
Merged

cleanup rive on unmount#366
bodymovin merged 1 commit into
mainfrom
NNNN-fix_leak_on_unmount

Conversation

@bodymovin

@bodymovin bodymovin commented Jun 18, 2025

Copy link
Copy Markdown
Contributor

we need to keep a reference of the latest rive file instance in a ref hook to make sure we destroy it on unmount or else it can cause a memory leak

Comment thread src/hooks/useRive.tsx
...riveParams,
canvas: canvasElem,
});
if (riveRef.current != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this hook we are already doing:

return () => {
      if (!isLoaded) {
        r?.cleanup();
      }
    };

Could it be that we're not ever calling cleanup under the condition where it failed (or did not complete) to load before the hook was unmounted (isLoaded == false)?

The useRef seems like a better approach, but will we now be calling cleanup twice? And is that a problem?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I. verified that calling cleanup twice is not an issue since we clean the file.
And yes, from all the debugging, this was happening in a scenario where the file is unmounted when it didn't finish loading and the on load event is not fired.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do we still need:

if (!isLoaded) {
        r?.cleanup();
      }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do in the (rare) scenario where it's not actually unmounted but we do trigger a new rive instance. The isLoaded should cleanup the previous one.

@HayesGordon HayesGordon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just the question about wether we should keep the !isLoaded conditional cleanup.

@bodymovin
bodymovin merged commit 7b174f7 into main Jun 18, 2025
1 check passed
@bodymovin
bodymovin deleted the NNNN-fix_leak_on_unmount branch June 18, 2025 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants