hi, I'm looking at the docs and am not fully sure ...
# kotlin-native
b
hi, I'm looking at the docs and am not fully sure how Kotlin native works in the memory department; what values require manual management and which ones are handled by a GC?
e
Kotlin objects are all GC'able (except when pinned). C interop requires manual management
b
let's say you read in values from C and put them into a dataclass,what do you need to do
same vice versa
do you need to copy those?
e
it depends*
read in values from C
sounds like a copy to begin with though
and exposing Kotlin objects to C always involves copying or pinning
b
pinning is basically once it goes out of scope it gets cleaned up? similar to Rust's Drop?
e
no
think like JNI
b
no experience with that unfortunately
e
when an object is pinned, you are telling the garbage collector: "there is some reference to this object that you do not know about, do not touch this object"
it is up to you to unpin the object when you're done or else it is a leak
b
I see
as for getting values from C; let's assume that you read in a jpg file using some C lib and it passes you a byte array
the array is malloced ofc
how would you transfer that into a kotlin data class
I suppose you don't want to copy that
e
in most situations I would make a full copy to a Kotlin ByteArray or similar, so that it will work with all other existing Kotlin functions and types