I noticed the following example in the nim manual to demonstrate how to use an
untraced object:
type
Data = tuple[x, y: int, s: string]
# allocate memory for Data on the heap:
var d = cast[ptr Data](alloc0(sizeof(Data)))
# create a new string on the garbage collected heap:
d.s = "abc"
# tell the GC that the string is not needed anymore:
GCunref(d.s)
# free the memory:
dealloc(d)
Run
But when I test the code, I found that even if I've commented `GCunref(d.s)`
out, no memory leak will happen (according to memory usage by `top`). Below is
my full test code:
proc foobar(i:int) =
type
Data = tuple[x, y: int, s: string]
# allocate memory for Data on the heap:
var d = cast[ptr Data](alloc0(sizeof(Data)))
# create a new string on the garbage collected heap:
# use a different string everytime to prevent any "cache" if possible
d.s = $i
# tell the GC that the string is not needed anymore:
#GCunref(d.s)
# free the memory:
dealloc(d)
for i in 0..1000000000:
foobar(i)
Run
I've tried to find out what happend by looking at definations of functions like
`unsureAsgnRef` etc. in the source code, but they all give me the impression
that memory leak should happen here. Have I got something wrong? Or GCunref()
isn't necessary in this case?