I am currently debugging an issue in owlkettle and manage to reduce to this minimal example.
var p: proc()
proc createCallback(x: int): proc() =
result = proc() =
for it in 0..<100:
p = createCallback(it)
echo x
for it in 0..<10:
p = createCallback(10)
p()
Running it with nim compile -r test3 outputs:
99
99
99
99
99
99
99
99
99
99
Running it with nim compile -d:useMalloc -r test3 outputs:
10
99
99
99
99
99
99
99
99
99
Running it with nim compile --mm:boehm -r test3 outputs:
10
10
10
10
10
10
10
10
10
10
Running it with nim js test3.nim; node test3.js outputs:
10
10
10
10
10
10
10
10
10
10
In case version info is relevant:
$ nim --version
Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-10-14
Copyright (c) 2006-2023 by Andreas Rumpf
git hash: f5d70e7fa7195658e3200f71a1653e07fe81275a
active boot switches: -d:release
What is the expected output? Is it just undefined behavior?
In general: Should running a procedure prevent the deallocation of its closure?