Skip to content

Conversation

@caio-pizzol
Copy link
Contributor

This commit refines the cleanup process in the SuperDocTemplateBuilder component by ensuring that the instance is properly referenced before calling the destroy method. It also resets additional references to prevent memory leaks.

This commit refines the cleanup process in the SuperDocTemplateBuilder component by ensuring that the instance is properly referenced before calling the destroy method. It also resets additional references to prevent memory leaks.
@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 enabled auto-merge (squash) October 9, 2025 19:32
@caio-pizzol caio-pizzol merged commit 01f0bf9 into main Oct 9, 2025
2 checks passed
@caio-pizzol caio-pizzol deleted the cursor/SD-543-fix-memory-leak-clear-trigger-refs-92f6 branch October 9, 2025 19:32
@superdoc-bot
Copy link

superdoc-bot bot commented Oct 9, 2025

🎉 This PR is included in version 0.2.0-next.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

superdoc-bot bot pushed a commit that referenced this pull request Nov 13, 2025
# [0.2.0](v0.1.0...v0.2.0) (2025-11-13)

### Bug Fixes

* force pre-release ([f752754](f752754))
* improve cleanup logic ([#11](#11)) ([01f0bf9](01f0bf9))
* update field ID type and improve field handling ([#14](#14)) ([e0e6d31](e0e6d31))
* update field IDs and categories in README and App component ([61a473d](61a473d))

### Features

* add import functionality for .docx files in the template builder ([#15](#15)) ([42faccc](42faccc))
* enhance exportTemplate functionality with configurable options ([#17](#17)) ([7e2a03d](7e2a03d))
* enhance field handling with mode support in template builder ([#16](#16)) ([d46ab5d](d46ab5d))
* implement viewport clamping for menu positioning in SuperDocTemplateBuilder ([#10](#10)) ([09e82ee](09e82ee))
@superdoc-bot
Copy link

superdoc-bot bot commented Nov 13, 2025

🎉 This PR is included in version 0.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants