Crash when instantiating hybrid ObjC/Java class from Swift

Hi there,

I’ve been having a bit of trouble with interop between hybrid Java/ObjC classes and Swift. A sample project demonstrating the problem can be found here.

In this example, I have a class ImageProvider which is declared in ObjC and implemented in Kotlin. Using this class in ObjC is not a problem - this can be verified by changing the view controller in the main storyboard to ObjCViewController. However, using an (ostensibly the same) view controller written in Swift (ViewController) causes a crash when instantiating the ImageProvider. Am I doing something wrong, or is there some sort of special case when using these kinds of classes from Swift?

If anyone has any suggestions I would be hugely appreciative. Thanks for your help!
-Damian

Here’s a screenshot of the debugger… if it helps any. It is not very informative unfortunately.

You need to add alloc() and init() methods. See here for a Kotlin example in one of our test-projects: https://github.com/squins/moe-non-freed-objects-issue/blob/master/src/main/kotlin/com/squins/reproduction/nonfreedobjects/ui/SecondScreen.kt

I tried that, and it doesn’t fix the problem. The problem appears when instantiating an instance of the class in Swift – not when doing so from Java or ObjC.

I’ve managed to work around the issue by adding a static factory method to the ObjC class which returns a new instance of the ImageProvider. Implementing this in either ObjC or Java works fine, and can be used in Swift just fine after the instantiation. It is only calling ImageProvider() in Swift that causes the crash.

Let me know if you have any idea, and thanks again,
-Damian

The question is what the Swift -> ObjC bridge does when you call ImageProvider(). Can we be certain, that it will call the alloc selector followed by the init selector on the returned object? The fact, that a static factory method works around the issue at least suggests, that something unexpected is happening during construction.

We are looking into the issue, we will keep you posted.

Best Regards,
Gergely

1 Like

Thanks @kisg, that is pretty much what I suspected. Thanks for letting me know; I will be on the lookout for any updates.

Cheers,
Damian

Dear Damian,

We have released MOE 1.3.2 yesterday and this should be fixed by now.

Best Regards,
Daniel

Hi Daniel,

That’s great news! Thanks for the quick turnaround. I will check it out.

Cheers,
Damian