Skip to content

[BUG] Memory leak in PanningManager: some listeners are not destroyed #994

Closed

Description

@wenkui0519

Memory leak in PanningManager due to missing cleanup of mouseUpListener after graph destruction.

Environment:"@maxgraph/core": "^0.22.0"

Code location:
PanningManager.js
this.mouseUpListener = () => { if (this.active) this.stop(); };

Image

Activity

  1. added
    bug
    triage
    on Jan 15, 2026
  2. changed the title [-][BUG] Memory leak in PanningManager.js[/-] [+][BUG] Memory leak in PanningManager: lsome listener [/+] on Jan 15, 2026
  3. changed the title [-][BUG] Memory leak in PanningManager: lsome listener [/-] [+][BUG] Memory leak in PanningManager: some listeners are not destroyed[/+] on Jan 15, 2026
  4. tbouffard commented on Jan 15, 2026

    @tbouffard
    Member

    Hi
    can you please confirm that your graph is using the PanningHandler plugin, and that you see the problem when you call the destroy method of the graph, the listener is not removed?
    If possible, please provide a code snippet

    My first analysis shows that PanningHandler.onDestroy is not calling the destroy method of PanningManager.

    Note: This problem already existed in mxGraph 4.2.2: https://github.com/jgraph/mxgraph/blob/v4.2.2/javascript/src/js/handler/mxPanningHandler.js#L488-L494

  5. added this to the 0.23.0 milestone on Jan 15, 2026
  6. wenkui0519 commented on Jan 16, 2026

    @wenkui0519
    Author

    Hello, my function is like this: When pressing Ctrl or Space, I can drag the canvas with the left mouse button.
    Here is my code:

    onInit() {
      plugins = [PanningHandler,....];
      this.graph = new graph(container, plugins);
    }
    // drag function
    private dragFunc(){
      const panningHandler: PanningHandler = this.graph.getPlugin('PanningHandler');
      const keydownFunction = (e) => {
          if (e.code === 'Space' || e.code === 'ControlLeft') {
              if (!this.disabled) {
                  this.graph.setEnabled(false);
              }
              e.preventDefault();
              e.stopPropagation();
              this.graph.container.style.cursor = 'grab';
              panningHandler.useLeftButtonForPanning = true; // 左键触发平移
          }
      };
      const keyupFunction = (e) => {
          if (e.code === 'Space' || e.code === 'ControlLeft') {
              if (!this.disabled) {
                  this.graph.setEnabled(true);
              }
              e.preventDefault();
              e.stopPropagation();
              this.graph.container.style.cursor = 'default';
              panningHandler.useLeftButtonForPanning = false; // 恢复默认
          }
      };
      document.addEventListener('keydown', keydownFunction);
      document.addEventListener('keyup', keyupFunction);
      // when component destroyed
      this.destroy$.subscribe(() => {
          document.removeEventListener('keydown', keydownFunction);
          document.removeEventListener('keyup', keyupFunction);
      });
    }
    // destroy component
    onDestroy(): void {
          this.graph.destroy();
          this.destroy$.next();
          this.destroy$.complete();
      }

    When I destroy the graph, the destroy function in PanningManager.js is not executed.

  7. tbouffard commented on Jan 16, 2026

    @tbouffard
    Member

    Thanks for the code snippet and the explanation.
    This is appreciated and will help for the fix.

  8. removed
    bug
    triage
    on Feb 23, 2026
  9. added theissue type on Feb 23, 2026
  10. self-assigned this
    on Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions