diff --git a/components/core/services/resize.ts b/components/core/services/resize.ts index a1429c4ba30..ee9b8462b8a 100644 --- a/components/core/services/resize.ts +++ b/components/core/services/resize.ts @@ -3,7 +3,7 @@ * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ -import { Injectable, NgZone, Renderer2, RendererFactory2 } from '@angular/core'; +import { Injectable, NgZone, OnDestroy, Renderer2, RendererFactory2 } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { auditTime, finalize } from 'rxjs/operators'; @@ -12,7 +12,7 @@ const NOOP = (): void => {}; @Injectable({ providedIn: 'root' }) -export class NzResizeService { +export class NzResizeService implements OnDestroy { private readonly resizeSource$ = new Subject(); private listeners = 0; @@ -31,6 +31,12 @@ export class NzResizeService { this.renderer = this.rendererFactory2.createRenderer(null, null); } + ngOnDestroy(): void { + // Caretaker note: the `handler` is an instance property (it's not defined on the class prototype). + // The `handler` captures `this` and prevents the `NzResizeService` from being GC'd. + this.handler = NOOP; + } + subscribe(): Observable { this.registerListener();