-
Notifications
You must be signed in to change notification settings - Fork 26k
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
Factory provider type does not respect OnDestroy hook #22240
Comments
For now this works as intended as we check the presence of |
@vicb What about the issue with the router? The router class implements the |
Duplicate of #14818. Better to file a separate issue for router memory leak problem. |
Previously, ngOnDestroy was only called on services which were statically determined to have ngOnDestroy methods. In some cases, such as with services instantiated via factory functions, it's not statically known that the service has an ngOnDestroy method. This commit changes the runtime to look for ngOnDestroy when instantiating all DI tokens, and to call the method if it's present. Fixes angular#22466 Fixes angular#22240 Fixes angular#14818
Previously, ngOnDestroy was only called on services which were statically determined to have ngOnDestroy methods. In some cases, such as with services instantiated via factory functions, it's not statically known that the service has an ngOnDestroy method. This commit changes the runtime to look for ngOnDestroy when instantiating all DI tokens, and to call the method if it's present. Fixes angular#22466 Fixes angular#22240 Fixes angular#14818
Previously, ngOnDestroy was only called on services which were statically determined to have ngOnDestroy methods. In some cases, such as with services instantiated via factory functions, it's not statically known that the service has an ngOnDestroy method. This commit changes the runtime to look for ngOnDestroy when instantiating all DI tokens, and to call the method if it's present. Fixes #22466 Fixes #22240 Fixes #14818 PR Close #23755
@vicb this also doesn't work with an InjectionToken which surprised me very much. Surely that can be figured out at compile time?
If I switch this to the following then my
I can see how it's much more complicated with a factory - but surely this should work? |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
If we provide a service with a
ngOnDestroy
method as a type provider the service'sngOnDestroy
lifecycle hook is called. If however the same service is provided via a factory function, thengOnDestroy
lifecycle hook is never called.Expected behavior
The behavior should not differ if using a type provider or a factory provider.
Minimal reproduction of the problem with instructions
Simple Demo: https://stackblitz.com/edit/angular-3azkmn
If you comment in the type provider, the
ngOnDestroy
hook is called (console.log is performed).If you use the factory provider the
ngOnDestroy
hook is not calledWhat is the motivation / use case for changing the behavior?
I have tried to destroy an angular application manually (by calling the NgModuleRef.destroy method) that contains an angular router. After the appliction is destroyed the routing events still fire. The cause for this is that the
ngOnDestroy
hook is never called. Further this leads to a memory leak, as the application is not garbage collected.Other issues might be that someone changes the provider type and is not aware of this limitation. Thus introducing subtle bugs into the appliction.
Solution
I do not exactly know where to look for a fix, but the
nodeDef.flags
do not contain theOnDestroy
bit. So the cause of this bug is probably somwhere in the compiler, which I am not too familiar with.Environment
The text was updated successfully, but these errors were encountered: