-
Notifications
You must be signed in to change notification settings - Fork 156
fix(template-outlet): memory leak for cached templates #15608
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
Open
Parent:
Mass merging 20.1.x to master
pmoleri
wants to merge
6
commits into
20.1.x
Choose a base branch
from
pmoleri/fix-template-outlet-leak
base: 20.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8e6141
fix(template-outlet): memory leak for cached templates
pmoleri f1dbfc1
Merge branch 'master' into pmoleri/fix-template-outlet-leak
kdinev cc57b19
Merge remote-tracking branch 'origin/20.1.x' into pmoleri/fix-templat…
ChronosSF 8d28d5b
Merge branch '20.1.x' into pmoleri/fix-template-outlet-leak
ChronosSF 3c9bc9b
Merge branch '20.1.x' into pmoleri/fix-template-outlet-leak
ChronosSF 01640e8
Merge branch '20.1.x' into pmoleri/fix-template-outlet-leak
ChronosSF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { | ||
| Directive, EmbeddedViewRef, Input, OnChanges, ChangeDetectorRef, | ||
| SimpleChange, SimpleChanges, TemplateRef, ViewContainerRef, NgZone, Output, EventEmitter | ||
| SimpleChange, SimpleChanges, TemplateRef, ViewContainerRef, NgZone, Output, EventEmitter, | ||
| OnDestroy, | ||
| } from '@angular/core'; | ||
|
|
||
| import { IBaseEventArgs } from '../../core/utils'; | ||
|
|
@@ -12,7 +13,7 @@ import { IBaseEventArgs } from '../../core/utils'; | |
| selector: '[igxTemplateOutlet]', | ||
| standalone: true | ||
| }) | ||
| export class IgxTemplateOutletDirective implements OnChanges { | ||
| export class IgxTemplateOutletDirective implements OnChanges, OnDestroy { | ||
| @Input() public igxTemplateOutletContext !: any; | ||
|
|
||
| @Input() public igxTemplateOutlet !: TemplateRef<any>; | ||
|
|
@@ -51,6 +52,10 @@ export class IgxTemplateOutletDirective implements OnChanges { | |
| } | ||
| } | ||
|
|
||
| public ngOnDestroy(): void { | ||
| this.cleanCache(); | ||
| } | ||
|
|
||
| public cleanCache() { | ||
| this._embeddedViewsMap.forEach((collection) => { | ||
| collection.forEach((item => { | ||
|
|
@@ -63,15 +68,6 @@ export class IgxTemplateOutletDirective implements OnChanges { | |
| this._embeddedViewsMap.clear(); | ||
| } | ||
|
|
||
| public cleanView(tmplID) { | ||
| const embViewCollection = this._embeddedViewsMap.get(tmplID.type); | ||
| const embView = embViewCollection?.get(tmplID.id); | ||
| if (embView) { | ||
| embView.destroy(); | ||
| this._embeddedViewsMap.get(tmplID.type).delete(tmplID.id); | ||
| } | ||
| } | ||
|
|
||
| private _recreateView() { | ||
| const prevIndex = this._viewRef ? this._viewContainerRef.indexOf(this._viewRef) : -1; | ||
| // detach old and create new | ||
|
|
@@ -88,12 +84,7 @@ export class IgxTemplateOutletDirective implements OnChanges { | |
| // if context contains a template id, check if we have a view for that template already stored in the cache | ||
| // if not create a copy and add it to the cache in detached state. | ||
| // Note: Views in detached state do not appear in the DOM, however they remain stored in memory. | ||
| const resCollection = this._embeddedViewsMap.get(this.igxTemplateOutletContext['templateID'].type); | ||
| const res = resCollection?.get(this.igxTemplateOutletContext['templateID'].id); | ||
| if (!res) { | ||
| this._embeddedViewsMap.set(this.igxTemplateOutletContext['templateID'].type, | ||
| new Map([[this.igxTemplateOutletContext['templateID'].id, this._viewRef]])); | ||
| } | ||
| this.cacheView(tmplId, this._viewRef); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -105,7 +96,7 @@ export class IgxTemplateOutletDirective implements OnChanges { | |
| if (view !== this._viewRef) { | ||
| if (owner._viewContainerRef.indexOf(view) !== -1) { | ||
| // detach in case view it is attached somewhere else at the moment. | ||
| this.beforeViewDetach.emit({ owner: this, view: this._viewRef, context: this.igxTemplateOutletContext }); | ||
| this.beforeViewDetach.emit({ owner: this, view, context: this.igxTemplateOutletContext }); | ||
| owner._viewContainerRef.detach(owner._viewContainerRef.indexOf(view)); | ||
| } | ||
| if (this._viewRef && this._viewContainerRef.indexOf(this._viewRef) !== -1) { | ||
|
|
@@ -197,6 +188,26 @@ export class IgxTemplateOutletDirective implements OnChanges { | |
| return TemplateOutletAction.UpdateViewContext; | ||
| } | ||
| } | ||
|
|
||
| private cacheView(tmplID: { type: string, id: unknown } | undefined, viewRef: EmbeddedViewRef<any>) { | ||
| if (!tmplID) { | ||
| return; | ||
| } | ||
|
|
||
| const hasUniqueId = tmplID.id !== undefined && tmplID.id !== null; | ||
| if (hasUniqueId) { | ||
| // Don't cache locally unique id views, they're handled by the parent component by using moveview instead of cache | ||
| return; | ||
| } | ||
|
|
||
| let resCollection = this._embeddedViewsMap.get(tmplID.type); | ||
| if (!resCollection) { | ||
| resCollection = new Map(); | ||
| this._embeddedViewsMap.set(tmplID.type, resCollection); | ||
| } | ||
|
|
||
| resCollection.set(tmplID.id, viewRef); | ||
|
||
| } | ||
| } | ||
| enum TemplateOutletAction { | ||
| CreateView, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a more explicit check for unique IDs. The current logic treats
tmplID.id = 0ortmplID.id = falseas non-unique, which may not be the intended behavior if these are valid ID values.