I have two methods below:
function foobar() {
const viewState = getViewState();
return () => { use(viewState); }
}
function foobar() {
return (viewState) => { use(viewState); }
}
Can you please let me know if there is memory leak from any of these functions?
I was told by someone that the 1st method is leaking since the viewState is passed from outside and it is being held inside the closure without being released, whereas the 2nd function is not leaking because viewState is passed as a parameter.
If that's true, I am interested in more detailed explanation.
- Why the 1st method is leaking memory? Is it because the program is unable to remove viewState from memory automatically after it executes the code inside the arrow function?
- Why the 2nd method where passing viewState as parameter is not causing memory leak?