Skip to content

fix(memory leak): memory leaking in UniqueSelectionDispatcher #5164

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

Merged
merged 5 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
HostBinding,
Input,
OnInit,
OnDestroy,
Optional,
Output,
QueryList,
Expand Down Expand Up @@ -271,7 +272,7 @@ export class MdButtonToggleGroupMultiple extends _MdButtonToggleGroupMixinBase
'class': 'mat-button-toggle'
}
})
export class MdButtonToggle implements OnInit {
export class MdButtonToggle implements OnInit, OnDestroy {
/** Whether or not this button toggle is checked. */
private _checked: boolean = false;

Expand All @@ -287,6 +288,9 @@ export class MdButtonToggle implements OnInit {
/** Whether or not the button toggle is a single selection. */
private _isSingleSelector: boolean = null;

/** Unregister function for _buttonToggleDispatcher **/
private _removeUniqueSelectionListener: () => void = () => {};

@ViewChild('input') _inputElement: ElementRef;

/** The parent button toggle group (exclusive selection). Optional. */
Expand Down Expand Up @@ -372,11 +376,12 @@ export class MdButtonToggle implements OnInit {
this.buttonToggleGroupMultiple = toggleGroupMultiple;

if (this.buttonToggleGroup) {
_buttonToggleDispatcher.listen((id: string, name: string) => {
if (id != this.id && name == this.name) {
this.checked = false;
}
});
this._removeUniqueSelectionListener =
_buttonToggleDispatcher.listen((id: string, name: string) => {
if (id != this.id && name == this.name) {
this.checked = false;
}
});

this._type = 'radio';
this.name = this.buttonToggleGroup.name;
Expand Down Expand Up @@ -446,4 +451,9 @@ export class MdButtonToggle implements OnInit {
event.value = this._value;
this.change.emit(event);
}

// Unregister buttonToggleDispatcherListener on destroy
ngOnDestroy(): void {
this._removeUniqueSelectionListener();
}
}
32 changes: 32 additions & 0 deletions src/lib/core/coordination/unique-selection-dispatcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {UniqueSelectionDispatcher} from './unique-selection-dispatcher';


describe('Unique selection dispatcher', () => {

describe('register', () => {
it('once unregistered the listener must not be called on notify', (done) => {
let dispatcher: UniqueSelectionDispatcher = new UniqueSelectionDispatcher();
let called = false;

// Register first listener
dispatcher.listen(() => {
called = true;
});

// Register a listener
let deregisterFn = dispatcher.listen(() => {
done.fail('Should not be called');
});

// Unregister
deregisterFn();

// Call registered listeners
dispatcher.notify('testId', 'testName');

expect(called).toBeTruthy('Registered listener must be called.');

done();
});
});
});
12 changes: 10 additions & 2 deletions src/lib/core/coordination/unique-selection-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ export class UniqueSelectionDispatcher {
}
}

/** Listen for future changes to item selection. */
listen(listener: UniqueSelectionDispatcherListener) {
/**
* Listen for future changes to item selection.
* @return Function used to unregister listener
**/
listen(listener: UniqueSelectionDispatcherListener): () => void {
this._listeners.push(listener);
return () => {
this._listeners = this._listeners.filter((registered: UniqueSelectionDispatcherListener) => {
return listener !== registered;
});
Copy link
Member

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

Copy link
Contributor Author

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.

};
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/lib/expansion/accordion-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,24 @@ export class AccordionItem implements OnDestroy {
}
private _expanded: boolean;

/** Unregister function for _expansionDispatcher **/
private _removeUniqueSelectionListener: () => void = () => {};

constructor(@Optional() public accordion: CdkAccordion,
protected _expansionDispatcher: UniqueSelectionDispatcher) {
_expansionDispatcher.listen((id: string, accordionId: string) => {
if (this.accordion && !this.accordion.multi &&
this.accordion.id === accordionId && this.id !== id) {
this.expanded = false;
}
});
this._removeUniqueSelectionListener =
_expansionDispatcher.listen((id: string, accordionId: string) => {
if (this.accordion && !this.accordion.multi &&
this.accordion.id === accordionId && this.id !== id) {
this.expanded = false;
}
});
}

/** Emits an event for the accordion item being destroyed. */
ngOnDestroy() {
this.destroyed.emit();
this._removeUniqueSelectionListener();
}

/** Toggles the expanded state of the accordion item. */
Expand Down
15 changes: 10 additions & 5 deletions src/lib/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ export class MdRadioButton extends _MdRadioButtonMixinBase
/** Reference to the current focus ripple. */
private _focusRipple: RippleRef;

/** Unregister function for _radioDispatcher **/
private _removeUniqueSelectionListener: () => void = () => {};

/** The native `<input type=radio>` element */
@ViewChild('input') _inputElement: ElementRef;

Expand All @@ -473,11 +476,12 @@ export class MdRadioButton extends _MdRadioButtonMixinBase
// TODO(jelbourn): Assert that there's no name binding AND a parent radio group.
this.radioGroup = radioGroup;

_radioDispatcher.listen((id: string, name: string) => {
if (id != this.id && name == this.name) {
this.checked = false;
}
});
this._removeUniqueSelectionListener =
_radioDispatcher.listen((id: string, name: string) => {
if (id != this.id && name == this.name) {
this.checked = false;
}
});
}

/** Focuses the radio button. */
Expand Down Expand Up @@ -513,6 +517,7 @@ export class MdRadioButton extends _MdRadioButtonMixinBase

ngOnDestroy() {
this._focusOriginMonitor.stopMonitoring(this._inputElement.nativeElement);
this._removeUniqueSelectionListener();
}

/** Dispatch change event with current value. */
Expand Down