Skip to content

Commit 8ea5d6d

Browse files
committedSep 13, 2024··
fix(reactivity): properly clean up deps, fix memory leak
close #11901
1 parent 11eebcb commit 8ea5d6d

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed
 

‎packages/reactivity/src/effect.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,23 @@ export class ReactiveEffect<T = any>
208208
}
209209
}
210210

211+
/**
212+
* For debugging
213+
*/
214+
// function printDeps(sub: Subscriber) {
215+
// let d = sub.deps
216+
// let ds = []
217+
// while (d) {
218+
// ds.push(d)
219+
// d = d.nextDep
220+
// }
221+
// return ds.map(d => ({
222+
// id: d.id,
223+
// prev: d.prevDep?.id,
224+
// next: d.nextDep?.id,
225+
// }))
226+
// }
227+
211228
let batchDepth = 0
212229
let batchedEffect: ReactiveEffect | undefined
213230

@@ -265,9 +282,11 @@ function cleanupDeps(sub: Subscriber) {
265282
// Cleanup unsued deps
266283
let head
267284
let tail = sub.depsTail
268-
for (let link = tail; link; link = link.prevDep) {
285+
let link = tail
286+
while (link) {
287+
const prev = link.prevDep
269288
if (link.version === -1) {
270-
if (link === tail) tail = link.prevDep
289+
if (link === tail) tail = prev
271290
// unused - remove it from the dep's subscribing effect list
272291
removeSub(link)
273292
// also remove it from this effect's dep list
@@ -281,6 +300,7 @@ function cleanupDeps(sub: Subscriber) {
281300
// restore previous active link if any
282301
link.dep.activeLink = link.prevActiveLink
283302
link.prevActiveLink = undefined
303+
link = prev
284304
}
285305
// set the new head & tail
286306
sub.deps = head

0 commit comments

Comments
 (0)
Please sign in to comment.