-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
fix(memory leak): memory leaking in UniqueSelectionDispatcher #5164
Conversation
Please merge ASAP: it is causing huge mem-leaks in my application so it makes the Chrome tab to crash in few minutes... In combination with #5144 I'm almost unable to use the library. We are managing a big data set that is leaking because of these... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Just a few comments
* Listen for future changes to item selection. | ||
* @return Function used to unregister listener | ||
**/ | ||
listen(listener: UniqueSelectionDispatcherListener): Function { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type is () => void
@@ -305,6 +306,9 @@ export class MdButtonToggle implements OnInit { | |||
/** Whether or not the button toggle is a single selection. */ | |||
private _isSingleSelector: boolean = null; | |||
|
|||
/** Unregister function for _buttonToggleDispatcherListener **/ | |||
private _buttonToggleDispatcherListener: Function; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call this _removeUniqueSelectionListener
, type is () => void
and initialize it to () => {}
:
private _removeUniqueSelectionListener: () => void = () => {};
Then you don't have to check if it is defined in the ngOnDestroy
(same change for other components as well)
@devversion this is potentially a good case for a mixin in a follow-up PR
this.checked = false; | ||
} | ||
}); | ||
this._buttonToggleDispatcherListener = _buttonToggleDispatcher.listen( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer line-breaking at the higher syntactic level, e.g.
this._removeUniqueSelectionListener =
_buttonToggleDispatcher.listen((id: string, name: string) => {
if (id != this.id && name == this.name) {
this.checked = false;
}
});
return () => { | ||
this._listeners = this._listeners.filter((registered: UniqueSelectionDispatcherListener) => { | ||
return listener !== registered; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs a unit test. It should set up a listener, then deregister and assert that listener isn't called again on notify
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. See unique-selection-dispatcher.spec.ts.
@jelbourn All comments are implemented. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Listeners are only registered inside of
UniqueSelectionDispatcher
. But there were never removed causing in my case a small memory leak when usingMdButtonToggle
.In this fix
UniqueSelectionDispatcher.listen
returns a function that removes the listener once called.