Hey, is anyone able to give me an example on how t...
# kotlin-native
b
Hey, is anyone able to give me an example on how to create a variable that would conform to this type?
value: kotlinx.cinterop.CValuesRef<platform.posix.uint8_tVar>
ideally I'd like something like
value="ASCDF"
d
cValues<uint8_tVar>('A'.toByte(), ......)
or
"ASCDF".cstr.getPointer(....).reinterpret<uint8_tVar>()
b
What is AutofreeScope? How do I create/get an instance of that?
n
Note that
CValuesRef
will accept a
CPointer
,
CArrayPointer
but not a
CValue
. Think of the Ref part of
CValuesRef
providing the hint that it is dealing with a Pointer, or anything that is similar to a Pointer (eg
CArrayPointer
).
AutofreeScope is a contiguous area where memory is allocated, and can be freed as a single block. There are two types that derive from AutofreeScope, MemScope and Arena. Most of the time you will want to use MemScope like the following: fun myFunc() = memScoped { // Allocated temporary memory here. // Do some stuff with the allocated memory. } Note that MemScope will automatically free memory when the end of the scope (code block) is reached, so you need to be very careful with memory lifetimes.
b
Interesting Nick, what are the possible side effects of MemScope freeing memory early? This could be the cause of some bugs we are encountering that we aren't seeing in native swift development
d
use-after-free is undefined behaviour.
👍 1
n
Here is a good example of a use after free situation: https://kotlinlang.slack.com/archives/C3SGXARS6/p1639023942371900