From ad21ee20aefb8d0744e9098b565422d987c808ed Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 13 Jan 2022 16:33:04 +0100 Subject: [PATCH] fix(table): not clearing some internal references on destroy (#16051) `CdkTable` keeps track of various definitions internally in order to render itself, however some of them weren't being cleared on destroy which could lead to memory leaks. These changes add some extra logic to clear the tracked references. (cherry picked from commit caf979a1defe2c2085887beb2dfce7bc68d6366a) --- src/cdk/table/table.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/cdk/table/table.ts b/src/cdk/table/table.ts index 3ed73c43b66d..91b0129b047a 100644 --- a/src/cdk/table/table.ts +++ b/src/cdk/table/table.ts @@ -611,13 +611,23 @@ export class CdkTable implements AfterContentChecked, CollectionViewer, OnDes } ngOnDestroy() { - this._rowOutlet.viewContainer.clear(); - this._noDataRowOutlet.viewContainer.clear(); - this._headerRowOutlet.viewContainer.clear(); - this._footerRowOutlet.viewContainer.clear(); - - this._cachedRenderRowsMap.clear(); + [ + this._rowOutlet.viewContainer, + this._headerRowOutlet.viewContainer, + this._footerRowOutlet.viewContainer, + this._cachedRenderRowsMap, + this._customColumnDefs, + this._customRowDefs, + this._customHeaderRowDefs, + this._customFooterRowDefs, + this._columnDefsByName + ].forEach(def => { + def.clear(); + }); + this._headerRowDefs = []; + this._footerRowDefs = []; + this._defaultRowDef = null; this._onDestroy.next(); this._onDestroy.complete();