Skip to content

Conversation

@caio-pizzol
Copy link
Contributor

Clear trigger-related refs and SuperDoc instance on unmount to prevent memory leaks.

When the component unmounts, especially during active menu interaction, lingering references in triggerCleanupRef and menuTriggerFromRef can hold closures that reference unmounted DOM elements or editor instances, leading to memory leaks. This change ensures these references are explicitly nulled out.


Linear Issue: SD-543

Open in Cursor Open in Web

Co-authored-by: caiopizzol <caiopizzol@gmail.com>
@cursor
Copy link

cursor bot commented Oct 9, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@linear
Copy link

linear bot commented Oct 9, 2025

SD-543 Fix memory leak: clear trigger refs on unmount

Summary

Ensure cleanup refs are cleared on unmount to prevent memory leaks when component is destroyed during active menu interaction

Tasks

  • Add cleanup for triggerCleanupRef.current in main useEffect return
  • Add cleanup for menuTriggerFromRef.current in main useEffect return
  • Clear superdocRef.current references before setting to null on unmount
  • Test unmount during active menu state to verify no console warnings

Notes

Problem: When component unmounts while menu is open or trigger is active, refs hold closures that reference editor/DOM that no longer exist

Current code (src/index.tsx ~line 400):

return () => {
  if (superdocRef.current) {
    if (typeof superdocRef.current.destroy === 'function') {
      superdocRef.current.destroy();
    }
    superdocRef.current = null;
  }
};

Missing: Clear triggerCleanupRef.current and menuTriggerFromRef.current before destroying

Fix:

return () => {
  // Clear trigger refs
  triggerCleanupRef.current = null;
  menuTriggerFromRef.current = null;
  
  // Destroy editor
  if (superdocRef.current) {
    if (typeof superdocRef.current.destroy === 'function') {
      superdocRef.current.destroy();
    }
    superdocRef.current = null;
  }
};

Verification: Mount component → type trigger → unmount → verify no memory leaks in Chrome DevTools

@caio-pizzol caio-pizzol closed this Oct 9, 2025
@caio-pizzol caio-pizzol deleted the cursor/SD-543-fix-memory-leak-clear-trigger-refs-92f6 branch October 9, 2025 19:31
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.

3 participants