Skip to content
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(dialog): leaking component instance references #2875

Merged
merged 1 commit into from
Mar 17, 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
10 changes: 5 additions & 5 deletions src/lib/core/portal/portal-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
}

ngOnDestroy() {
this.dispose();
super.dispose();
}

/**
Expand All @@ -93,7 +93,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
componentFactory, viewContainerRef.length,
portal.injector || viewContainerRef.parentInjector);

this.setDisposeFn(() => ref.destroy());
super.setDisposeFn(() => ref.destroy());
return ref;
}

Expand All @@ -105,7 +105,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
portal.setAttachedHost(this);

this._viewContainerRef.createEmbeddedView(portal.templateRef);
this.setDisposeFn(() => this._viewContainerRef.clear());
super.setDisposeFn(() => this._viewContainerRef.clear());

// TODO(jelbourn): return locals from view
return new Map<string, any>();
Expand All @@ -114,11 +114,11 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
/** Detaches the currently attached Portal (if there is one) and attaches the given Portal. */
private _replaceAttachedPortal(p: Portal<any>): void {
if (this.hasAttached()) {
this.detach();
super.detach();
}

if (p) {
this.attach(p);
super.attach(p);
this._portal = p;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/lib/core/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ describe('Portals', () => {
expect(someDomElement.innerHTML)
.toBe('', 'Expected the DomPortalHost to be empty after detach');
});

it('should call the dispose function even if the host has no attached content', () => {
let spy = jasmine.createSpy('host dispose spy');

expect(host.hasAttached()).toBe(false, 'Expected host not to have attached content.');

host.setDisposeFn(spy);
host.dispose();

expect(spy).toHaveBeenCalled();
});
});
});

Expand Down
25 changes: 16 additions & 9 deletions src/lib/core/portal/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ export abstract class BasePortalHost implements PortalHost {
private _isDisposed: boolean = false;

/** Whether this host has an attached portal. */
hasAttached() {
return this._attachedPortal != null;
hasAttached(): boolean {
return !!this._attachedPortal;
}

attach(portal: Portal<any>): any {
if (portal == null) {
if (!portal) {
throw new NullPortalError();
}

Expand Down Expand Up @@ -195,24 +195,31 @@ export abstract class BasePortalHost implements PortalHost {
abstract attachTemplatePortal(portal: TemplatePortal): Map<string, any>;

detach(): void {
if (this._attachedPortal) { this._attachedPortal.setAttachedHost(null); }

this._attachedPortal = null;
if (this._disposeFn != null) {
this._disposeFn();
this._disposeFn = null;
if (this._attachedPortal) {
this._attachedPortal.setAttachedHost(null);
this._attachedPortal = null;
}

this._invokeDisposeFn();
}

dispose() {
if (this.hasAttached()) {
this.detach();
}

this._invokeDisposeFn();
this._isDisposed = true;
}

setDisposeFn(fn: () => void) {
this._disposeFn = fn;
}

private _invokeDisposeFn() {
if (this._disposeFn) {
this._disposeFn();
this._disposeFn = null;
}
}
}
1 change: 1 addition & 0 deletions src/lib/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class MdDialogRef<T> {
this._overlayRef.dispose();
this._afterClosed.next(this._result);
this._afterClosed.complete();
this.componentInstance = null;
}
});
}
Expand Down
13 changes: 13 additions & 0 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,19 @@ describe('MdDialog', () => {
});
});

it('should not keep a reference to the component after the dialog is closed', async(() => {
let dialogRef = dialog.open(PizzaMsg);

expect(dialogRef.componentInstance).toBeTruthy();

dialogRef.close();
viewContainerFixture.detectChanges();

viewContainerFixture.whenStable().then(() => {
expect(dialogRef.componentInstance).toBeFalsy('Expected reference to have been cleared.');
});
}));

describe('disableClose option', () => {
it('should prevent closing via clicks on the backdrop', () => {
dialog.open(PizzaMsg, {
Expand Down