-
-
Save runspired/b5e50a2a70b6308ee560 to your computer and use it in GitHub Desktop.
class Foo { | |
constructor(element) { | |
this.element = element; | |
this.setupHandlers(); | |
} | |
doFoo() {} | |
setupHandlers() { | |
this.listener = () => { | |
this.doFoo(); | |
} | |
this.element.addEventListener('scroll', this.listener, true); | |
} | |
destroy() { | |
this.teardownHandlers(); | |
this.element = null; | |
} | |
teardownHandlers() { | |
this.element.removeEventListener('scroll', this.listener, true); | |
// without this next line, the `Foo` instance and any other scope present within the instance | |
// will leak, preventing garbage collection. | |
this.listener = null; | |
} | |
} |
I guess what I was getting at is it's not necessarily a leak with "arrow functions", specifically. It's more of a leak with that pattern.
Have you tried testing this in Chrome, without transpilation? It natively supports classes and arrow functions in strict mode. I would also recommend trying it isolated from the smoke-and-mirrors project, if you haven't already. From what has been described, I believe this should be reclaimed. ¯_(ツ)_/¯
Unless of course you're holding on to a reference of instance Foo
or this.listener
elsewhere..clarifying, just in case 😏
Unless of course you're holding on to a reference of instance Foo or this.listener elsewhere..clarifying, just in case
I assumed I was, and spent a huge amount of time digging through the heap and my code making sure things were properly dereferenced. It's still possible, but at this point highly improbable. I will be digging more.
I have not tested without transpilation.
Additionally, this is the exact code involved, and the commit which fixed the issue.
https://github.com/runspired/smoke-and-mirrors/blob/e3de4397ec53746ec935546b1c5a36a3afded814/addon/models/radar.js#L186-L226
Other leaks and other teardown needs were addressed in the commit as well, but all you need to replicate the leak is to remove this line: https://github.com/runspired/smoke-and-mirrors/blob/e3de4397ec53746ec935546b1c5a36a3afded814/addon/models/radar.js#L223
Steps to replicate
isDevelopingAddon
to returntrue
inindex.js
ember serve
infinite scroll
from the demos listvertical-collection
was the item being retained.demos
by clicking thedemos
link in the navbarAs you navigate around it's references, you'll notice a lot of the paths eventually go back to
filterMovement
as the closest thing to window (I think 5 levels deep if I recall?), this is the function that's invoked (via backburner, which is executing the loop in arequestAnimationFrame
).Now uncomment out the line that nulls the scroll handler, and repeat. No leak. :/