Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Strange crash #18

Closed
fab1an opened this issue May 17, 2016 · 7 comments
Closed

Strange crash #18

fab1an opened this issue May 17, 2016 · 7 comments

Comments

@fab1an
Copy link

fab1an commented May 17, 2016

I'm trying to deserialize a custom data-class with various members.

I just tried gson.fromJson<MyType>(...) without any custom deserializers, but I keep getting a strange segfault (libc, not an exception that I can catch).

When I call gson.fromJson<MyType>('{}') i get the correct type (with defaults set).

@SalomonBrys
Copy link
Owner

I have no idea.... Never encountered this.
There's no JNI neither in kotson nor in Gson, so this is a JVM bug...

@fab1an
Copy link
Author

fab1an commented May 17, 2016

I think it happens because the type of one of my members is an interface, so kotson/gson can't figure out the time. Maybe java tries to instantiate the kotlin interface and crashes?

@SalomonBrys
Copy link
Owner

I just tried this scenario, and it gives a proper exception RuntimeException : "Unable to invoke no-args constructor for interface com.github.salomonbrys.kotson.Itf. Register an InstanceCreator with Gson for this type may fix this problem."

@fab1an
Copy link
Author

fab1an commented May 17, 2016

Oh, I should have mentioned that I'm running on android. Big sorry. O.o.

This crashes:

/* MainActivity.java */

@Override
protected void onResume() {
   super.onResume();
   TestKt.restore(this);
}
/* Test.kt */

interface TestInterface {}

class TestClass() : TestInterface {}

data class Test(
    val member: TestInterface = TestClass()
)

fun restore(activity: Activity) {
    val test = Gson().fromJson<Test>("{\"member\":{}}");
}

Gives me:
Fatal signal 11 (SIGSEGV), code 1, fault addr 0x6f0095 in tid 29300 (fab1ans.app)

@fab1an
Copy link
Author

fab1an commented May 17, 2016

@SalomonBrys
Copy link
Owner

I've investigated based on the code you gave, and this is a Gson / Android issue.
It is not related to Kotlin or Kotson (I've replicated the same crash with only java code).

It is because Gson tries to instanciate an interface, (in this case, TestInterface). While the JVM would throw an exception, Android juste crashes...
It appears the issue has been fixed, and will be part of Gson's next release :)

However, even once the crash is fixed, it will still throw an issue: if you wish to deserialize an interface, you must register a deserializer! Gson has no way of knowing which concrete type it should create in place of the interface.

@SalomonBrys
Copy link
Owner

Great news ! Gson 2.7 is out with the aforementioned fix and can be used with Kotson 2.3.0 :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants