Nico Buescher
02/12/2019, 1:36 AMtypedef struct {
const char *str;
} Example;
What is the intended way to store a string literal in str
from Kotlin?
Using "String".cstr
doesn't work since str
is a CPointer<ByteVar>
, and String.cstr
is a CValues<ByteVar>
, and when I use memScoped { ... }
with getPointer(memScope)
, the CString is obviously freed as soon as the scope is finished, leaving me with an empty string in the struct member.Dico
02/12/2019, 4:50 AMmemScoped
block.olonho
02/12/2019, 6:07 AMfoo.cstr.getPointer(nativeHeap)
, as documented in https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md#working-with-the-stringsNico Buescher
02/12/2019, 1:24 PMNico Buescher
02/12/2019, 1:28 PMfoo.cstr.getPointer(nativeHeap)
doesn't work since getPointer(...)
expects an AutoFreeScope
, and nativeHeap is only a NativeFreeablePlacement
. Same with placeTo
. This is with kotlin 1.3.21. The docs seem to be a bit out of date.Dico
02/12/2019, 1:34 PMNico Buescher
02/12/2019, 2:02 PMolonho
02/12/2019, 2:02 PMval a = Arena()
val ptr = "Hello".cstr.getPointer(a)
// a.clear() when pointer no longer needed
Nico Buescher
02/12/2019, 2:06 PM