jwharm / java-gi

GObject-Introspection bindings generator for Java
GNU Lesser General Public License v2.1
84 stars 7 forks source link

TypeInstances other than GObject are never removed from InstanceCache #95

Closed BwackNinja closed 4 months ago

BwackNinja commented 5 months ago

Since commit https://github.com/jwharm/java-gi/commit/6326ea19daa9e82972893a1368c08735dd5b37df TypeInstances are successfully added to the InstanceCache. This is a problem because only GObjects have a ToggleNotify, which is the only way an instance is removed from the InstanceCache. This causes a java.lang.ClassCastException when an object of a different type occupies a previously used memory address because the old java object will be returned.

An example TypeInstance that isn't a GObject is a RenderNode. If they are not unref'd, then there will be a memory leak, but if they are then an exception will eventually occur.

I've patched it locally and it works for me, but haven't tried this with the development branch, which puts it into the strongReferences map now rather than the weakReferences map.

jwharm commented 4 months ago

Thanks for logging these issues.

I made some changes to remove non-GObject classes from the InstanceCache, but it is still work in progress.

jwharm commented 4 months ago

One problem that I haven't figured out yet, is how to treat interfaces. When a method returns an interface, is it a GObject or not? AFAIK that information is only available at runtime.

jwharm commented 4 months ago

I think this should work correctly now (in the development branch), but I'm having trouble creating testcases.

Do you have a testcase that I can use?

jwharm commented 4 months ago

I tested it with Gtk Expression and that works. This will be fixed in the next release.

BwackNinja commented 4 months ago

Fantastic. Thanks for resolving this so quickly.

I was having trouble thinking of a testcase that would reliably fail before your changes.