Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in Shortcuts class #2631

Open
melenudo opened this issue Feb 21, 2024 · 0 comments
Open

Memory leak in Shortcuts class #2631

melenudo opened this issue Feb 21, 2024 · 0 comments

Comments

@melenudo
Copy link

melenudo commented Feb 21, 2024

Editor.js Version

v2.29.0

Issue description

Hi!

I've found a memory leak in the Shortcuts class. When you remove the shortcut for an element you don't check if the element's shortcuts are empty and left the element in the registeredShortcuts map (with an empty array):

this.registeredShortcuts.set(element, shortcuts.filter(el => el !== shortcut));

If you create a new EditorJS and destroys it, you will see a bunch of detached elements referenced by registeredShortcuts map.

Steps to reproduce:

You can reproduce the issue with this simple html:

<html>
  <body>
    <button id="remove">Remove</button>
    <div id="editor" style="min-width: 700px"></div>
    <script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"></script>
    <script>
      let editor = new EditorJS({
        holder: 'editor',
      });

      function remove() {
        document.querySelector('#editor').remove();
        editor.isReady.then(() => {
          editor.destroy();
          editor = undefined;
        });
      }
      document.querySelector('#remove').addEventListener('click', remove);
    </script>
  </body>
</html>

Press "Remove" button and take a snapshot in Chrome dev tools. You will see detached HTMLDivElement's referenced by registeredShortcuts:

screenshot

If you remove the element from the map when the shortcuts array is empty, the problem disappears:

const newShortcuts = shortcuts.filter(el => el !== shortcut);

newShortcuts.length===0?
  this.registeredShortcuts.delete(element):
  this.registeredShortcuts.set(element, newShortcuts);

screenshot2

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

No branches or pull requests

2 participants