fix: null out effect.b in destroy_effect to prevent memory leak

pull/17980/head
Nitin Sahu 1 month ago
parent 425fba33fe
commit 509868bc41

@ -0,0 +1,9 @@
---
'svelte': patch
---
fix: null out `effect.b` (boundary reference) in `destroy_effect` to prevent memory leak
When a dynamic component switches (e.g., `<Component />`), old branch effects are destroyed but were still retaining a reference to the `Boundary` instance via the `effect.b` field. This prevented the garbage collector from reclaiming memory for destroyed component subtrees, causing memory to grow without bound when repeatedly changing a mounted component's props.
Fixes #17881

@ -559,6 +559,7 @@ export function destroy_effect(effect, remove_dom = true) {
effect.fn =
effect.nodes =
effect.ac =
effect.b =
null;
}

@ -0,0 +1,26 @@
import { flushSync } from 'svelte';
export default {
html: '<p>Home</p>',
async test({ assert, target, component }) {
// Switch to About
component.page = 'About';
flushSync();
assert.htmlEqual(target.innerHTML, '<p>About</p>');
// Switch back to Home
component.page = 'Home';
flushSync();
assert.htmlEqual(target.innerHTML, '<p>Home</p>');
// Rapidly switch multiple times to stress test cleanup
for (let i = 0; i < 10; i++) {
component.page = i % 2 === 0 ? 'About' : 'Home';
flushSync();
}
// Should end on Home (10 is even, 0-indexed last is About → Home)
assert.htmlEqual(target.innerHTML, '<p>Home</p>');
}
};

@ -0,0 +1,10 @@
<script>
import Home from './Home.svelte';
import About from './About.svelte';
let { page = 'Home' } = $props();
const components = { Home, About };
let Component = $derived(components[page]);
</script>
<Component />
Loading…
Cancel
Save