godot-kotlin-jvm icon indicating copy to clipboard operation
godot-kotlin-jvm copied to clipboard

Find a way to handle Godot objects bound to different Kotlin instances.

Open CedNaru opened this issue 2 years ago • 0 comments

This one is a bit of a mindfuck for me so I might be confusing.

Let's say we have a regular Godot Object without script sent to the JVM as a parameter. What happens currently is that we check whether this pointer already exists in our GarbageCollector hashmap so we can retrieve the JVM instance otherwise we create it.

Now this Godot Object got a JVM wrapper instance. Sometimes later, the user decides to attach to that object a Kotlin Script ( A core idea of Godot is that you can at runtime add/remove/change the script of any Object to change their behavior). Now we are going to run into an issue because we are going to create a new JVM instance for that script instance. This means that potentially we can have 2 different Kotlin instances of different types for the same pointer (the native class and the script class) eixisting at the same time in the JVM. Of course, we can't magically replace current references to the native class by references to the script. So the 2 must coexist somehow. This part would require testing but I suspect that in the case of Recounted based instances, we may get early deletion of the Godot Object.

Ex: Some Kotlin class holds an instance of the native object class type and another holds an instance of the script class type. At some point, the native object is not used anymore and garbage collected, it would then decrement its counter on the C++ side despite still having the script instance alive.

For now, I have no clue how to handle that. I first need to see how the new memory management model behave first. But putting that issue here to not forget it's a thing.

CedNaru avatar Sep 22 '21 01:09 CedNaru