Thomas
07/25/2019, 3:38 PMmalloc: *** error for object 0x280872d78: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug
I am guessing I am doing something wrong with my cinterop code. How can I find the cause of the issue in my code?Artyom Degtyarev [JB]
07/26/2019, 6:39 AMThomas
07/26/2019, 8:21 AMimport kotlinx.cinterop.CPointer
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.get
import kotlinx.cinterop.usePinned
import platform.Foundation.NSData
import platform.Foundation.create
import platform.posix.uint8_tVar
fun ByteArray.toNSData(): NSData = usePinned { pinned ->
return NSData.create(
bytesNoCopy = pinned.addressOf(0),
length = size.toULong()
)
}
fun NSData.toByteArray(): ByteArray {
@Suppress("UNCHECKED_CAST")
val bytePtr = (bytes as CPointer<uint8_tVar>)
return ByteArray(length.toInt()) { index ->
bytePtr[index].toByte()
}
}
Thomas
07/27/2019, 8:28 AMbytesNoCopy
with bytes
, which fixed the issue. This is probably because NSData
with bytesNoCopy
expects the ByteArray
to be stable in memory, even after the create function. Is that right?svyatoslav.scherbina
07/29/2019, 8:57 AM