ngParty / ng-metadata

Angular 2 decorators and utils for Angular 1.x

Home Page:https://hotell.gitbooks.io/ng-metadata/content/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

$destroy event on $element not firing causing leaks

reppners opened this issue · comments

The $destroy-event fired on IAugmentedJQuery(https://docs.angularjs.org/api/ng/function/angular.element#events) when they are removed from DOM is not fired when using ng-metadata. This leads to all sorts of leaks because logic that does cleanup e.g. unregistering $watches are not executed.

https://embed.plnkr.co/wHLoWOJ3who4vmqLu0Bd/

I agree that $destroy probably should be fired, but is there a reason ngOnDestroy() is not sufficient? Like do you know if this causes memory leaks with other frameworks?

Yes, ng Material ist leaking in the rootscope. Im in the process of migrating a large app where I recognized leaking watches that weren't occurring before migrating to ng metadata. This is a showstopper until resolved :(

The $rootScope.$watch leak on ng-material originates from the $mdTheming-service. Every component that has some theming will register itself on the $mdTheming-service where a $watch is registered on the $rootScope when either the md-theme-watch directive is used on the element or when $mdThemingProvider.alwaysWatchTheme(true); is configured.

https://github.com/angular/material/blob/v1.1.1/src/core/services/theming/theming.js#L578

Our app relies on theming and there is also a solution in place for generating theme-css for custom components which replicates how ng-material is doing it internally.

Any hint on how to approach a fix is appreciated.

Possible source location

@Hotell
Is the element.off() there for a particular reason or is it a kind of precaution?

element.off() seems to be unnecessary because its done automatically on element remove by jqLite
https://github.com/angular/angular.js/blob/d14c7f3c31deb098bf8f1c50ea6d00af758dbdcb/src/jqLite.js#L363