Xcode just updated and now I get a crash at runtim...
# kotlin-native
t
Xcode just updated and now I get a crash at runtime when running my iOS app.
Copy code
failed to demangle superclass of MainViewController from mangled name '13MyProject22BaseViewControllerCySo16MyItemCSo0G24MyPresenter_pG'
This is the original line in Swift:
Copy code
class MainViewController: BaseViewController<MyItem, MyPresenter>, MyView
(MyItem, MyPresenter, MyView are Kotlin classes) I found this in the release notes under "Known Issues" (https://developer.apple.com/documentation/xcode_release_notes/xcode_10_2_release_notes/swift_5_release_notes_for_xcode_10_2):
Copy code
Linking against a static Swift library might create a binary with missing type metadata because the object files that define the metadata inside the static archive are mistakenly considered unused. (47598583)

This can manifest as a Swift runtime error with a message such as: “failed to demangle superclass of MyClass from mangled name ‘<mangled name>’”.

Workaround: If you can rebuild the static library, try building it with whole module optimization enabled. Otherwise, add -all_load to the linker flags in the client binary to ensure all object files are linked into it.
I tried adding the all_load linker flag to my Xcode project but there was no difference, but I might have added it wrong as I am very new to Xcode. Could anyone help me out?
s
Where is
BaseViewController
declared? Is it the same Swift module? Would the crash happen if no Kotlin classes were involved?
Have you tried doing a clean build?
t
@svyatoslav.scherbina
BaseViewController
is located in the same Swift module. I've tried a clean build as well but still got the crash.
If I remove this class the crash just happens at a next class in my code. I have removed all Swift classes from my project which reference Kotlin code and now it doesn't crash. As soon as I try to add the class again it crashes.
Looks like it only happens if I reference a Kotlin class as a generic parameter. For example, this code does not crash:
Copy code
class MainViewController: UIViewController, MyView
(MyView is a Kotlin interface)
s
This issue looks much like a bug in interop between Swift and Objective-C. It is reproducible without integrating any Kotlin code into the project.
👍 2
Please try the following workaround: add
Copy code
let kotlinProtocols = [Foo.self, Bar.self]
to any Swift file, where
Foo
and
Bar
are Kotlin interfaces used as generic parameters.
🎉 1
t
Thanks a lot for looking into this, really appreciate your help. I'll try the workaround in a few hours and will let you know.
👍 1
s
Thanks a lot for reporting the issue! We still have some time to incorporate a workaround into upcoming Kotlin 1.3.30 release, so I would really appreciate your feedback on this.
t
@svyatoslav.scherbina I tried out the workaround and can confirm it works! After adding every Kotlin interface which was used as a generic parameter the app no longer crashes. Thank you very much!
s
Thank you for the confirmation!
Do you get the same error with Xcode 10.2 and Kotlin 1.3.40?
t
I am already using some iOS 13 features so I can not open my project in Xcode 10.2. For some reason I’m not able to reproduce it in a new project. Not sure why this is happening
s
I have successfully reproduced the issue again, and it doesn’t seem to be directly related to Xcode 11.
t
Great, let me know if I can do anything.
l
Is there an issue we can follow?
s
Please ensure that you have your Kotlin framework in “Linked Frameworks and Libraries” section under “General” tab of target settings.
t
@svyatoslav.scherbina Like this?
common.framework
is the Kotlin framework.
s
It is possible that Xcode doesn’t consider this framework required. Please ensure that you have the framework enumerated under “Link Binary With Libraries” build phase of your application.
t
Please ensure that you have the framework enumerated under “Link Binary With Libraries” build phase of your application.
@svyatoslav.scherbina These are the build phases in my project
s
Do you get the same error if you use any class from Kotlin framework in your application Swift code? E.g. add
print(Foo())
, where
Foo
in declared in Kotlin.
t
@svyatoslav.scherbina No, only if I use a Kotlin interface as a generic parameter of a class in Swift. Exactly like the issue I reported a few months ago.
s
So do I understand correctly that adding usage of Kotlin class also helps to workaround the issue?
If by ‘usage’ you meant adding the Kotlin interfaces somewhere in the swift code like above
s
No. By workaround here https://kotlinlang.slack.com/archives/C3SGXARS6/p1561035136201500?thread_ts=1553600522.440400&amp;cid=C3SGXARS6 I mean doing the following: 1. Add
class Foo
to your Kotlin code. It must be a
class
, not an
interface
. 2. Add top-level property
let foo = Foo.self
to your Swift code.
t
I followed your steps: I added
class Foo
to my Kotlin code. Then I added
let foo = Foo.self
at the top of a file in my Swift code. The app works fine in the simulator and does not crash on start.
s
Then this confirms that the issue is that Xcode doesn’t link the framework in your project explicitly. Not sure whether it is an unwanted Xcode optimization or your project misconfiguration, but there is likely nothing Kotlin/Native can do to provide better workaround for the original Swift bug. Thanks for the details.
👍 1
t
Thanks for your help!
🙏 1