Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngRepeat): fix trackBy function being invoked with incorrect scope - #16777

Merged
jbedard merged 2 commits into
angular:masterfrom
jbedard:16776
Dec 6, 2018
Merged

fix(ngRepeat): fix trackBy function being invoked with incorrect scope#16777
jbedard merged 2 commits into
angular:masterfrom
jbedard:16776

Conversation

@jbedard

@jbedard jbedard commented Nov 29, 2018

Copy link
Copy Markdown
Contributor

This also fixes a leak of that scope across all further instances of the
repeated element.

Fixes #16776

What is the current behavior? (You can also link to an open issue here)
The track-by function would get created on the first linking of an ng-repeat and then get cached forever, so each time that ng-repeat was re-linked (when it is repeated, or destroyed+recreated via ng-if etc.) the previous scope would be used. This also "leaks" the scope for the lifetime of that compiled node.

What is the new behavior (if this is a feature change)?
The track-by no longer directly references the scope

@gkalpak gkalpak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread test/ng/directive/ngRepeatSpec.js Outdated
Comment thread test/ng/directive/ngRepeatSpec.js
Comment thread test/ng/directive/ngRepeatSpec.js Outdated
@gkalpak

gkalpak commented Nov 29, 2018

Copy link
Copy Markdown
Member

For future reference, this was broken in bdd853c.

@jbedard
jbedard force-pushed the 16776 branch 2 times, most recently from 0070020 to 5819a21 Compare November 29, 2018 19:09
jbedard added a commit to jbedard/angular.js that referenced this pull request Nov 29, 2018
This also fixes a leak of that scope across all further instances of the
repeated element.

Fixes angular#16776
Closes angular#16777
Comment thread src/ng/directive/ngRepeat.js
jbedard added a commit to jbedard/angular.js that referenced this pull request Nov 30, 2018
Also fixes a leak of that scope across all further instances of the
repeated element.

Fixes angular#16776
Closes angular#16777
Comment thread src/ng/directive/ngRepeat.js Outdated

// Clear the value property from the hashFnLocals object to prevent a reference to the last value
// being leaked into this ngRepeatCompile function scope
hashFnLocals[valueIdentifier] = undefined;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about clearing the value here? This way we don't have to touch the trackByIdExpFn method that gets invoked on each element and just add this one assignment at the end.

Comment thread src/ng/directive/ngRepeat.js Outdated
Comment thread src/ng/directive/ngRepeat.js Outdated
// Clear the value property from the hashFnLocals object to prevent a reference to the last value
// being leaked into this ngRepeatCompile function scope
if (hashFnLocals) {
hashFnLocals[valueIdentifier] = undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about keyIdentifier? Can't is be non-primitive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keyIdentifier value is either an index or the itemKey from for (var itemKey in collection). So it should always be a number or string.

@jbedard

jbedard commented Dec 1, 2018

Copy link
Copy Markdown
Contributor Author

The build is all green 😲

@jbedard

jbedard commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

I rebased just to add "Ref #16776" to the second commit message, and got the build green again!

@jbedard
jbedard merged commit de0aad8 into angular:master Dec 6, 2018
jbedard added a commit that referenced this pull request Dec 6, 2018
Also fixes a leak of that scope across all further instances of the
repeated element.

Fixes #16776
Closes #16777
mgol pushed a commit that referenced this pull request Dec 6, 2018
Also fixes a leak of that scope across all further instances of the
repeated element.

Fixes #16776
Closes #16777
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ngRepeat "track by" possible memory leak

4 participants