https://kotlinlang.org logo
#ios
Title
# ios
t

Tim Hauptmann

10/17/2019, 12:12 PM
Hey, when I run the following code on an iOS simulator useCompanion() will produce a runtime crash. The method useInner() works perfect. Furthermore, this only happens when I inherit from UIScrollView. Is this wrongly used? Or is this a bug I should report?
import kotlinx.cinterop.CValue
import kotlinx.cinterop.cValue
import kotlinx.cinterop.useContents
import platform.CoreGraphics.CGRect
import platform.UIKit.NSDirectionalEdgeInsetsMake
import platform.UIKit.UIScrollView
class Test(frame: CValue<CGRect>) : UIScrollView(frame) {
private val innerInsets = NSDirectionalEdgeInsetsMake(top = 15.0, leading = 8.0, bottom = 15.0, trailing = 8.0)
companion object {
private val companionInsets = NSDirectionalEdgeInsetsMake(top = 15.0, leading = 8.0, bottom = 15.0, trailing = 8.0)
}
fun useInner(): Double {
return innerInsets.useContents { top } //This line works
}
fun useCompanion(): Double {
return companionInsets.useContents { top } //This line produces runtime crash on iOS Simulator: Thread 1: EXC_BAD_ACCESS
}
}
a

Artyom Degtyarev [JB]

10/17/2019, 12:27 PM
Hello! Can you add some more details on the crash?
t

Tim Hauptmann

10/17/2019, 12:45 PM
I use this class to call the code in Xcode in the AppDelegate. Because of: „Test is unavailable: Kotlin subclass of Objective-C class can't be imported“
class TestFactory {
fun useInner(): Double {
return Test(cValue<CGRect>()).useInner()
}
fun useCompanion(): Double {
return Test(cValue<CGRect>()).useCompanion()
}
}
I get the following stack trace with the crash
Thread 1 Queue : com.apple.main-thread (serial)
#0	0x000000180000000e in 0x180000000e ()
#1	0x000000010ccf39de in kfun:kotlinx.cinterop.placeTo@kotlinx.cinterop.CValues<#GENERIC_kotlinx.cinterop.CVariable>.(kotlinx.cinterop.AutofreeScope)Generic at /Users/teamcity/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt:129
#2	0x000000010ce15426 in kfun:kotlinx.cinterop.<get-pointed>@kotlinx.cinterop.CPointer<#GENERIC_kotlinx.cinterop.CPointed>.()Generic [inlined] at /Users/teamcity/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt:323
#3	0x000000010ce153ee in <inlined-out:<anonymous>> [inlined] at /my/path/Test.kt:38
#4	0x000000010ce153ee in <inlined-out:<anonymous>_1> [inlined] at /my/path/Test.kt:38
#5	0x000000010ce153ee in kfun:kotlinx.cinterop.memScoped(kotlin.Function1<kotlinx.cinterop.MemScope,#GENERIC>)Generic [inlined] at /Users/teamcity/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt:629
#6	0x000000010ce153af in kfun:kotlinx.cinterop.useContents@kotlinx.cinterop.CValue<#GENERIC_kotlinx.cinterop.CStructVar>.(kotlin.Function1<#GENERIC_kotlinx.cinterop.CStructVar,#GENERIC>)Generic [inlined] at /Users/teamcity/buildAgent/work/4d622a065c544371/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt:312
#7	0x000000010ce15325 in kfun:my.package.Test.useCompanion()ValueType at /my/path/Test.kt:23
#8	0x000000010ce15c3a in kfun:my.package.TestFactory.useCompanion()ValueType at /my/path/Test.kt:35
#9	0x000000010ce314dd in ___lldb_unnamed_symbol185$$my_project_name ()
#10	0x000000010c6fe982 in AppDelegate.application(_:didFinishLaunchingWithOptions:) at /my/path/AppDelegate.swift:97
This only happens for the variant inside the companion object. If I remove the subclassing from UIScrollview, the error doesn’t happen. Maybe it’s because of the subclassing from an Objective-C class. But I did not tried with other Objective-C classes yet.
a

Artyom Degtyarev [JB]

10/17/2019, 2:10 PM
Thank you for details. We have successfully reproduced this behaviour. Now please wait for updates.
👍 1
t

Tim Hauptmann

10/17/2019, 2:12 PM
Ok thanks for the quick response👍
2 Views