question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hammerjs events are causing memory leaks (detached nodes)

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When angular removes the event listener for a hammer event, it only calls the .off() function of the hammer manager instance.

addEventListener(element: HTMLElement, eventName: string, handler: Function): Function { const zone = this.manager.getZone(); eventName = eventName.toLowerCase();

return zone.runOutsideAngular(() => {
  // Creating the manager bind events, must be done outside of angular
  const mc = this._config.buildHammer(element);
  const callback = function(eventObj: HammerInput) {
    zone.runGuarded(function() { handler(eventObj); });
  };
  mc.on(eventName, callback);
  return () => mc.off(eventName, callback); <----- right here
});

}

However, this is not destroying the manager instance, which is holding a reference to the element. This is causing detached nodes for every element that has a @HostListener listening for a hammer touch event.

Expected behavior

When removing an event listener for a hammer touch event, no detached nodes should be left over. Therefore, in addition to .off() Angular should also call destroy().

Minimal reproduction of the problem with instructions

Here is a simple demo reproducing the problem. https://stackblitz.com/edit/angular-j95qco

  • Repeatedly clicking the ‘toggle button’ button will add and remove a my-button component with an HostListener on a tab event.
  • After that, go to the Memory tab in Chrome Dev Tools and create a heap snapshot.
  • Filter the list for detached.
  • You will see a bunch of detached nodes. Expand the nodes to see that my-button is causing it.

I also added a quick fix. Go to gesture.service.ts and un-comment the code between line 31 and 39. The fix simply calls .destroy() ind addition to .off()

What is the motivation / use case for changing the behavior?

The current behavior drastically decreases performance of apps with lot of touch events.

Environment


Angular version: 5.0.0


Browser:
- [X] Chrome (desktop) version 63

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
BaHXeLiSiHgcommented, Aug 20, 2018

Looking for this fix too, is it still not merged??

1reaction
SebastianSchenkcommented, Aug 13, 2018

What’s the status about the PR? Is there a reason it is not getting merged?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Causes of Memory Leaks in JavaScript and How to Avoid Them
Common Sources of Memory Leaks in JavaScript Code # · 1. Accidental global variables · 2. Closures · 3. Timers · 4. Event...
Read more >
How to Detect and Fix Memory Leaks With Chrome DevTools
Here are a few things that might cause memory leaks in an Angular app: Missing unsubscription, which will retain the components in the...
Read more >
Debug DOM memory leaks with the Detached Elements tool
Detached elements aren't always an indication of a memory leak, and memory leaks aren't always caused by detached elements. For long-running ...
Read more >
Memory leak caused by event listeners attached to knockout ...
This is causing a memory leak as more and more event listeners are left for elements that no longer exist on the page....
Read more >
How can Detached DOM elements cause memory leak in ...
DOM is like an double-linked tree which means a reference to a node in the tree will halt the entire tree from garbage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found