Krystian
12/22/2023, 10:30 PMval Texture.width: Int
get() = memScoped {
val w = alloc<IntVar>()
SDL_QueryTexture(this@width, null, null, w.ptr, null)
w.value
}
val Texture.width: Int
get() {
val byte = ByteArray(Int.SIZE_BYTES)
SDL_QueryTexture(this@width, null, null, byte.usePinned { it.addressOf(0).reinterpret() }, null)
return byte.usePinned {
it.addressOf(0).pointed.value.toInt()
}
}
Both are working fine and as intended, they return the width size of the texture, however. I'm curious about using usePinned example and is it actually safe to use? At this point the value is fully managed by Kotlin GC, isn't it?
Am I safe to assume it's okay for me to do this?Adam S
12/22/2023, 10:41 PMmemScoped {}
seems okay to me. I'd be cautious about using usePinned {}
like that. I'd try and do all operations within a single scope, and use an IntVar rather than a ByteArray, something like this:
val Texture.width: Int
get() {
return memScoped {
alloc<IntVar>().usePinned { intVar ->
SDL_QueryTexture(this@width, null, null, intVar.get().ptr, null)
intVar.get().value
}
}
}
Does that seem like it would work for you?Krystian
12/22/2023, 10:45 PMAdam S
12/22/2023, 10:49 PMusePinned {}
. Your original code will usually work, but there might be an edge case where the memory location moves between usePinned {}
invocations.Krystian
12/22/2023, 11:21 PMAdam S
12/23/2023, 10:15 PM