Hi, since upgrading to `1.3.60`, my iOS applicatio...
# kotlin-native
m
Hi, since upgrading to
1.3.60
, my iOS application crashes with one of the following assert from memory.cpp: - RuntimeAssert "Must be positive" - RuntimeAssert "cycle collector shall only work with single object containers" Sadly, I am not able to pinpoint or reproduce this issue with sample code. The only stack I have is the GarbageCollector failing. Any hints on what or how to investigate would be much appreciated. More info in the thread.
I could not reproduce the issue with Kotlin 1.3.50. I suspect NSData -> KotlinByteArray conversion might be a root cause (but it might not be related since this code runs in production for more than a month):
Copy code
object DataConverter {
    @ExperimentalUnsignedTypes
    fun convert(data: NSData): ByteArray {
        val memScopedData = memScoped { data }
        val byteArray = ByteArray(memScopedData.length.toInt()).apply {
            usePinned {
                memcpy(it.addressOf(0), memScopedData.bytes, memScopedData.length)
            }
        }
        return byteArray
    }

    @ExperimentalUnsignedTypes
    fun convert(byteArray: ByteArray): NSData {
        return memScoped {
            NSData.create(bytesNoCopy = allocArrayOf(byteArray),
                length = byteArray.size.toULong())
        }
    }
}
Swift code:
Copy code
extension Data {
    func toKotlinByteArray() -> KotlinByteArray {
        return DataConverter().convert(data: self)
    }
}

extension KotlinByteArray {
    func toNSData() -> Data {
        return DataConverter().convert(byteArray: self)
    }
}
o
val memScopedData = memScoped { data }
is pretty much meaningless. What are you tryng to do here?
smth like
Copy code
object DataConverter {
    @ExperimentalUnsignedTypes
    fun convert(data: NSData): ByteArray {
        val byteArray = ByteArray(data.length.toInt()).apply {
            usePinned {
                memcpy(it.addressOf(0), data.bytes, data.length)
            }
        }
        return byteArray
    }
    @ExperimentalUnsignedTypes
    fun convert(byteArray: ByteArray): NSData {
        return byteArray.usePinned {
            NSData.create(bytes = it.addressOf(0),
                    length = byteArray.size.toULong())
        }
    }
}
shall work
m
Crap, I definitely misused memscoped there. However, I'm still getting the assert (It might not be related to this block of code). Is there any flag I could add to the compiler to give me some more information about this particular object?
/Users/teamcity/buildAgent/work/4d622a065c544371/runtime/src/main/cpp/Memory.cpp:1091: runtime assert: Must be positive
o
this is an internal memory manager assert, usually a sign of memory corruption, you can try to run under some memory debugger, such as valgrind
m
Much appreciated, i’ll give it a try during the long weekend
j
This is most likly because you are trying to convert or fit data into the wrong structure, at least that was the case for me