Hi all, i am hunting a memory leak and suspsect it might be related to
refTo
.
I am passing a ByteArray reference by calling bytes.refTo to POSIX
read
and now wonder when the pinning will be undone? I see that the
CValuesRef
returned by
getPointer
takes an
AutofreeScope
.
Where does that come from? And when is it cleared?
I.e. what is the lifetime of a
CValuesRef
returned by
refTo
?
d
Dominaezzz
12/15/2022, 10:40 AM
The result of refTo is indefinitely valid, however when passed to a c interop function, the pointer seen by C is only valid during the function call.
u
uli
12/15/2022, 11:49 AM
thanks @Dominaezzz. that is the directly enclosing function call. as in:
Copy code
handleError(write(fd, buffer.refTo(0), 255))
the pointer could become invalid after `write`returns?and buffer can be garbage collected whenever it leaves it’s scope?
So the pointer C sees is generated automagically by the compiler by calling
getPointer
when it sees a CValueRef being passed? And the compiler also creates the AutofreeScope enclosing the c-interop-call
d
Dominaezzz
12/15/2022, 12:06 PM
That is correct.
Dominaezzz
12/15/2022, 12:08 PM
So refTo won't cause a memory leak.
u
uli
12/15/2022, 1:09 PM
Cool. Thanks again.
So now I only have to find my leak 🙂