Inheriting from custom type inheriting from Widget causes JVM crash #348

Closed
opened 2026-04-15 00:00:24 +02:00 by Til7701 · 5 comments

Firstly, thank you so much for this project. This is amazing.

When inheriting from a custom type that inherits from Widget crashes the JVM. The following snippets can be used to test this.

import org.gnome.gtk.Widget;

public class A extends Widget {
}
public class B extends A {
}
public class Main {
    static void main() {
        new B();
    }
}

Java-GI: 0.14.1
OS: Ubuntu 25.10
Gnome: 49

JVM output:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000077f8df73acf4, pid=160255, tid=160256
#
# JRE version: OpenJDK Runtime Environment (25.0.2+10) (build 25.0.2+10-Ubuntu-125.10)
# Java VM: OpenJDK 64-Bit Server VM (25.0.2+10-Ubuntu-125.10, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64)
# Problematic frame:
# C  [libgtk-4.so.1+0x33acf4]
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport -p%p -s%s -c%c -d%d -P%P -u%u -g%g -F%F -- %E" (or dumping to /home/tilman/IdeaProjects/java-gi-interitance-test/core.160255)
#
# An error report file with more information is saved as:
# hs_err_pid160255.log
[0.489s][warning][os] Loading hsdis library failed
#
# If you would like to submit a bug report, please visit:
#   https://bugs.launchpad.net/ubuntu/+source/openjdk-25
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

I originally got an error in a project where I tried to make a custom GtkListBoxRow as a base for more rows. In that case I constructed them using GtkBuilder and UI template files (I have a MemorySegment constructor). There I got:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.javagi.gobject.types.TypeRegistrationException: java.lang.NullPointerException: No memory layout for class class org.schlunzis.neuroevolution.view.components.LevelBarRow
	at org.javagi.gtk.types.TemplateTypes.registerTemplate(TemplateTypes.java:352)
	at org.javagi.gtk.types.TemplateTypes.register(TemplateTypes.java:369)
	at org.schlunzis.neuroevolution.Launcher.<clinit>(Launcher.java:21)
Caused by: java.lang.NullPointerException: No memory layout for class class org.schlunzis.neuroevolution.view.components.LevelBarRow
	at java.base/java.util.Objects.requireNonNull(Objects.java:246)
	at org.javagi.gtk.types.TemplateTypes.getTemplateInstanceLayout(TemplateTypes.java:129)
	at org.javagi.gtk.types.TemplateTypes.registerTemplate(TemplateTypes.java:299)
	... 2 more

I also tried to add getMemoryLayout methods to the classes. This changed the error message.

import org.gnome.gtk.Widget;

import java.lang.foreign.MemoryLayout;

public class CustomRow extends Widget {
    public static MemoryLayout getMemoryLayout() {
        return MemoryLayout.structLayout(
                Widget.getMemoryLayout().withName("parent_instance")
        ).withName("org_schlunzis_neuroevolution_view_components_CustomRow");
    }
}
import java.lang.foreign.MemoryLayout;

public class LevelBarRow extends CustomRow {

    public static MemoryLayout getMemoryLayout() {
        return MemoryLayout.structLayout(
                CustomRow.getMemoryLayout().withName("parent_instance")
        ).withName("org_schlunzis_neuroevolution_view_components_LevelBarRow");
    }

}
(process:157540): GLib-GObject-CRITICAL **: 23:37:49.492: specified instance size for type 'org_schlunzis_neuroevolution_view_components_LevelBarRow' is smaller than the parent type's 'org_schlunzis_neuroevolution_view_components_CustomRow' instance size
Firstly, thank you so much for this project. This is amazing. When inheriting from a custom type that inherits from Widget crashes the JVM. The following snippets can be used to test this. ```java import org.gnome.gtk.Widget; public class A extends Widget { } ``` ```java public class B extends A { } ``` ```java public class Main { static void main() { new B(); } } ``` Java-GI: 0.14.1 OS: Ubuntu 25.10 Gnome: 49 JVM output: ``` # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x000077f8df73acf4, pid=160255, tid=160256 # # JRE version: OpenJDK Runtime Environment (25.0.2+10) (build 25.0.2+10-Ubuntu-125.10) # Java VM: OpenJDK 64-Bit Server VM (25.0.2+10-Ubuntu-125.10, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64) # Problematic frame: # C [libgtk-4.so.1+0x33acf4] # # Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport -p%p -s%s -c%c -d%d -P%P -u%u -g%g -F%F -- %E" (or dumping to /home/tilman/IdeaProjects/java-gi-interitance-test/core.160255) # # An error report file with more information is saved as: # hs_err_pid160255.log [0.489s][warning][os] Loading hsdis library failed # # If you would like to submit a bug report, please visit: # https://bugs.launchpad.net/ubuntu/+source/openjdk-25 # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. # ``` I originally got an error in a project where I tried to make a custom `GtkListBoxRow` as a base for more rows. In that case I constructed them using GtkBuilder and UI template files (I have a MemorySegment constructor). There I got: ``` Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: org.javagi.gobject.types.TypeRegistrationException: java.lang.NullPointerException: No memory layout for class class org.schlunzis.neuroevolution.view.components.LevelBarRow at org.javagi.gtk.types.TemplateTypes.registerTemplate(TemplateTypes.java:352) at org.javagi.gtk.types.TemplateTypes.register(TemplateTypes.java:369) at org.schlunzis.neuroevolution.Launcher.<clinit>(Launcher.java:21) Caused by: java.lang.NullPointerException: No memory layout for class class org.schlunzis.neuroevolution.view.components.LevelBarRow at java.base/java.util.Objects.requireNonNull(Objects.java:246) at org.javagi.gtk.types.TemplateTypes.getTemplateInstanceLayout(TemplateTypes.java:129) at org.javagi.gtk.types.TemplateTypes.registerTemplate(TemplateTypes.java:299) ... 2 more ``` I also tried to add `getMemoryLayout` methods to the classes. This changed the error message. ```java import org.gnome.gtk.Widget; import java.lang.foreign.MemoryLayout; public class CustomRow extends Widget { public static MemoryLayout getMemoryLayout() { return MemoryLayout.structLayout( Widget.getMemoryLayout().withName("parent_instance") ).withName("org_schlunzis_neuroevolution_view_components_CustomRow"); } } ``` ```java import java.lang.foreign.MemoryLayout; public class LevelBarRow extends CustomRow { public static MemoryLayout getMemoryLayout() { return MemoryLayout.structLayout( CustomRow.getMemoryLayout().withName("parent_instance") ).withName("org_schlunzis_neuroevolution_view_components_LevelBarRow"); } } ``` ``` (process:157540): GLib-GObject-CRITICAL **: 23:37:49.492: specified instance size for type 'org_schlunzis_neuroevolution_view_components_LevelBarRow' is smaller than the parent type's 'org_schlunzis_neuroevolution_view_components_CustomRow' instance size ```
Owner

Thanks for reporting the issue!
I'll look into it.

Thanks for reporting the issue! I'll look into it.
Owner

Alright, I found the cause. Gtk isn't initialized.

Call Gtk.init() before creating the widget, and it should all work perfectly.

Alright, I found the cause. Gtk isn't initialized. Call `Gtk.init()` before creating the widget, and it should all work perfectly.
Author

That actually fixes the reproduction I provided, however, this does not fix my original problem. I am currently trying to make a minimal reproduction of my problem. I'll get back when I got it.

That actually fixes the reproduction I provided, however, this does not fix my original problem. I am currently trying to make a minimal reproduction of my problem. I'll get back when I got it.
Author

I have a more complex reproduction here: https://codeberg.org/Til7701/java-gi-inheritance-bug

Basically, I have two classes (A and B) that have a template and an instance is constructed from the window template. If A and B do not have a memory layout method, there is an error that A does not have a memory layout. So I copied and modified the method from GtkListBoxRow, but now it says that the layout for B is smaller than the one for A. This issue also only occurs, if B has a GtkChild taken from the template. In the new reproduction, it can be fixed by appending a ValueLayout.ADDRESS to the memory layout, but this does not work in my actual application. I do not know the difference to the reproduction yet.

I have a more complex reproduction here: https://codeberg.org/Til7701/java-gi-inheritance-bug Basically, I have two classes (`A` and `B`) that have a template and an instance is constructed from the window template. If `A` and `B` do not have a memory layout method, there is an error that A does not have a memory layout. So I copied and modified the method from `GtkListBoxRow`, but now it says that the layout for B is smaller than the one for A. This issue also only occurs, if B has a `GtkChild` taken from the template. In the new reproduction, it can be fixed by appending a `ValueLayout.ADDRESS` to the memory layout, but this does not work in my actual application. I do not know the difference to the reproduction yet.
Owner

Thanks for clarifying, that's indeed a bug. The memory layout for template classes is generated at runtime, but the derived class doesn't know about it and generates a wrong layout.

Thanks for clarifying, that's indeed a bug. The memory layout for template classes is generated at runtime, but the derived class doesn't know about it and generates a wrong layout.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
java-gi/java-gi#348
No description provided.