Thanks to visit codestin.com
Credit goes to github.com

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

ngSwitch attemps to remove DOM elements twice due to async iteration #8833

Conversation

thebigredgeek
Copy link
Contributor

Basically, I just added a check so that there isn't an attempted splice of the previousElement if it has already been removed by an earlier watch iteration between the time that the callback was defined and actually executed

@Narretz
Copy link
Contributor

Narretz commented Aug 29, 2014

I think this needs a test, no?

@IgorMinar
Copy link
Contributor

I don't think that this is right. It's the first removal without $animate that looks unnecessary.

@thebigredgeek
Copy link
Contributor Author

But doesn't removing that open the door to orphaned elements sitting inside of the previousElements array?

@thebigredgeek
Copy link
Contributor Author

Also, it appears that every single $animate.leave.then will attempt to splice off the same previousElements element, given that unless this is synchronous, i will always evaluate to selectedScopes.length - 1...

@thebigredgeek
Copy link
Contributor Author

When removing that bloc:

Chrome 37.0.2062 (Mac OS X 10.9.4) ngSwitch animations should destroy the previous leave     
animation if a new one takes place FAILED

I think that is intentional. I think the solution here is to check. It shouldn't splice an element that was already removed, and it shouldn't remove an element that was already spliced ;)

However, the callback handler is incredibly erroneous.

@IgorMinar
Copy link
Contributor

yeah. that's what I saw too. @matsko is on it and will correct the test if needed.

@thebigredgeek
Copy link
Contributor Author

So this should fix the closure issue. I do actually think that the $animate check is probably suboptimal in terms of CPU cycles, but does save a bit on memory. Just need a decision there ;)

@thebigredgeek
Copy link
Contributor Author

@IgorMinar any thoughts? I can go ahead and patch up whichever way you guys think we should go with this. Or was @matsko addressing this issue himself?

@IgorMinar
Copy link
Contributor

@matsko already landed the fix, but I think you are right about the i in splice being always the same number at the time the callback is evaluated.

Maybe you could submit revert your changes, rebase and fix that. Or submit a new PR and close this one.

@IgorMinar IgorMinar added this to the 1.3.0-rc.1 milestone Aug 30, 2014
@thebigredgeek
Copy link
Contributor Author

Sure. I am pretty sure his fix alone will actually cause a leak. This should patch it

@petebacondarwin
Copy link
Contributor

I don't think we should create that extra closure function inside the loop. Instead how about:

var spliceFactory = function(array, index) {
    return function() { array.splice(index, 1); };
};

for (i = 0, ii = selectedScopes.length; i < ii; ++i) {
  var selected = getBlockNodes(selectedElements[i].clone);
  selectedScopes[i].$destroy();
  promise.then(spliceFactory(previousLeaveAnimations, i));
}

Although maybe V8 et al are clever enough to optimize it out at runtime.

@petebacondarwin
Copy link
Contributor

Otherwise LGTM

@petebacondarwin petebacondarwin self-assigned this Sep 3, 2014
@petebacondarwin
Copy link
Contributor

I think this would not lead to a memory leak. The real problem would be that an animation may be cancelled twice if the wrong animation was spliced from the array. Still a problem but not as severe as a memory leak.

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.

5 participants