Skip to content

Commit deda3ec

Browse files
macjohnnymarjan-georgiev
authored andcommitted
fix(resizeable): fix memory leak due to created element not cleaned up (swimlane#1792)
1 parent 3372d63 commit deda3ec

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

projects/swimlane/ngx-datatable/src/lib/components/header/header.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
Input,
66
HostBinding,
77
ChangeDetectorRef,
8-
ChangeDetectionStrategy
8+
ChangeDetectionStrategy, OnDestroy
99
} from '@angular/core';
1010
import { MouseEvent } from '../../events';
1111
import { columnsByPin, columnGroupWidths, columnsByPinArr } from '../../utils/column';
@@ -68,7 +68,7 @@ import { translateXY } from '../../utils/translate';
6868
},
6969
changeDetection: ChangeDetectionStrategy.OnPush
7070
})
71-
export class DataTableHeaderComponent {
71+
export class DataTableHeaderComponent implements OnDestroy {
7272
@Input() sortAscendingIcon: any;
7373
@Input() sortDescendingIcon: any;
7474
@Input() scrollbarH: boolean;
@@ -158,8 +158,14 @@ export class DataTableHeaderComponent {
158158
right: {}
159159
};
160160

161+
private destroyed = false;
162+
161163
constructor(private cd: ChangeDetectorRef) {}
162164

165+
ngOnDestroy(): void {
166+
this.destroyed = true;
167+
}
168+
163169
onLongPressStart({ event, model }: { event: any; model: any }) {
164170
model.dragging = true;
165171
this.dragEventTarget = event;
@@ -303,7 +309,9 @@ export class DataTableHeaderComponent {
303309
this._styleByGroup.left = this.calcStylesByGroup('left');
304310
this._styleByGroup.center = this.calcStylesByGroup('center');
305311
this._styleByGroup.right = this.calcStylesByGroup('right');
306-
this.cd.detectChanges();
312+
if (!this.destroyed) {
313+
this.cd.detectChanges();
314+
}
307315
}
308316

309317
calcStylesByGroup(group: string): any {

projects/swimlane/ngx-datatable/src/lib/directives/resizeable.directive.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,30 @@ export class ResizeableDirective implements OnDestroy, AfterViewInit {
2929
element: HTMLElement;
3030
subscription: Subscription;
3131
resizing: boolean = false;
32+
private resizeHandle: HTMLElement;
3233

3334
constructor(element: ElementRef, private renderer: Renderer2) {
3435
this.element = element.nativeElement;
3536
}
3637

3738
ngAfterViewInit(): void {
3839
const renderer2 = this.renderer;
39-
const node = renderer2.createElement('span');
40+
this.resizeHandle = renderer2.createElement('span');
4041
if (this.resizeEnabled) {
41-
renderer2.addClass(node, 'resize-handle');
42+
renderer2.addClass(this.resizeHandle, 'resize-handle');
4243
} else {
43-
renderer2.addClass(node, 'resize-handle--not-resizable');
44+
renderer2.addClass(this.resizeHandle, 'resize-handle--not-resizable');
4445
}
45-
renderer2.appendChild(this.element, node);
46+
renderer2.appendChild(this.element, this.resizeHandle);
4647
}
4748

4849
ngOnDestroy(): void {
4950
this._destroySubscription();
51+
if (this.renderer.destroyNode) {
52+
this.renderer.destroyNode(this.resizeHandle);
53+
} else {
54+
this.renderer.removeChild(this.renderer.parentNode(this.resizeHandle), this.resizeHandle);
55+
}
5056
}
5157

5258
onMouseup(): void {

0 commit comments

Comments
 (0)