fixes #4851 [backport] (#24954)

(cherry picked from commit 1e602490e9)
This commit is contained in:
Andreas Rumpf
2025-05-16 09:44:13 +02:00
committed by narimiran
parent 8120d329ee
commit 17e0dae12f
4 changed files with 30 additions and 4 deletions

View File

@@ -252,12 +252,12 @@ iterator elementsExcept(t, s: CellSet): PCell {.inline.} =
var r = t.head
while r != nil:
let ss = cellSetGet(s, r.key)
var i:uint = 0
var i = 0'u
while int(i) <= high(r.bits):
var w = r.bits[i]
if ss != nil:
w = w and not ss.bits[i]
var j:uint = 0
var j = 0'u
while w != 0:
if (w and 1) != 0:
yield cast[PCell]((r.key shl PageShift) or

View File

@@ -597,7 +597,13 @@ proc sweep(gch: var GcHeap) =
if isCell(x):
# cast to PCell is correct here:
var c = cast[PCell](x)
if c notin gch.marked: freeCyclicCell(gch, c)
if c notin gch.marked:
# Don't free objects that have the ZctFlag set (created in finalizers)
if (c.refcount and ZctFlag) == 0:
freeCyclicCell(gch, c)
else:
# Clear the ZctFlag for the next collection cycle
c.refcount = c.refcount and not ZctFlag
proc markS(gch: var GcHeap, c: PCell) =
gcAssert isAllocatedPtr(gch.region, c), "markS: foreign heap root detected A!"

View File

@@ -83,7 +83,7 @@ proc runBasicDLLTest(c, r: var TResults, cat: Category, options: string, isOrc =
if "boehm" notin options:
# hcr tests
var basicHcrTest = makeTest("tests/dll/nimhcr_basic.nim", options & " --threads:off --forceBuild --hotCodeReloading:on " & rpath, cat)
# test segfaults for now but compiles:
if isOrc: basicHcrTest.spec.action = actionCompile
@@ -165,6 +165,7 @@ proc gcTests(r: var TResults, cat: Category, options: string) =
test "stackrefleak"
test "cyclecollector"
testWithoutBoehm "trace_globals"
test "tfinalizers"
# ------------------------- threading tests -----------------------------------

19
tests/gc/tfinalizers.nim Normal file
View File

@@ -0,0 +1,19 @@
type
PNode = ref TNode
TNode = object
le: PNode
proc finalizeNode(n: PNode) =
var s = @[0]
proc returnTree() =
var cycle: PNode
new(cycle, finalizeNode)
cycle.le = cycle
for i in 1..100:
returnTree()
GC_fullCollect()
GC_fullCollect()