Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JVM crashes on setting callback for GTK3 signals #118

Open
praj-foss opened this issue Nov 30, 2021 · 2 comments
Open

JVM crashes on setting callback for GTK3 signals #118

praj-foss opened this issue Nov 30, 2021 · 2 comments

Comments

@praj-foss
Copy link

Hello there!

I'm currently learning JNR by trying out various Linux libraries, most recently GTK3. I used this example as a reference and wrote the new demo that can be found here. But it crashes badly when I try to run it (using ./gradlew gtk3:run). Here's the crash log: hs_err_pid10169.log. I use GraalVM 21.3.0 as my JDK 11 on a x86_64 Linux machine (openSUSE tumbleweed). My installed GTK version is 3.24.30-2.3.

I can see that it crashes on line 31 of Gtk3App.java where I call from Java

lib.g_signal_connect_data(application, "activate", onActivate, null, null, 0);

The onActivate is a lambda looking like this:

LibGtk3.GCallback onActivate = (app, data) -> {
    var window = lib.gtk_application_window_new(app);
    var button = lib.gtk_button_new_wih_label("Click me");
    lib.gtk_container_add(window, button);
    lib.gtk_widget_show_all(window);
};

which is supposed to act like a function pointer similar to on_app_activate from my C reference:

// callback function which is called when application is first started
static void on_app_activate(GApplication *app, gpointer data) {
    // create a new application window for the application
    // GtkApplication is sub-class of GApplication
    // downcast GApplication* to GtkApplication* with GTK_APPLICATION() macro
    GtkWidget *window = gtk_application_window_new(GTK_APPLICATION(app));
    // a simple push button
    GtkWidget *btn = gtk_button_new_with_label("Click Me!");
    // connect the event-handler for "clicked" signal of button
    g_signal_connect(btn, "clicked", G_CALLBACK(on_button_clicked), NULL);
    // add the button to the window
    gtk_container_add(GTK_CONTAINER(window), btn);
    // display the window
    gtk_widget_show_all(GTK_WIDGET(window));
}

You can use https://github.com/praj-foss/jnr-demo to reproduce it using ./gradlew gtk3:run. The crash depends on the system as well: it ran perfectly fine on openSUSE leap 15.2, Mac OS, and Fedora Core 34 (verified by @headius and @enebo), but it crashed on openSUSE Tumbleweed and Ubuntu 21.10.

NOTE: This issue was earlier opened in jnr/jnr-ffi#281

@headius
Copy link
Member

headius commented Nov 30, 2021

Note the difference between this issue and jnr/jnr-ffi#281 is that the latter turned out to be mostly due to a mistake in the client code that caused a bad pointer to be passed out to C. Fixing that fixed the issue for me and @enebo but exposed a new crash within jffi for @praj-foss. I asked them to re-open as a jffi issue so we could investigate the problem.

@headius
Copy link
Member

headius commented Nov 30, 2021

Since we are not currently able to reproduce this issue, I repost what I know about enabling a debug build of jffi:

I believe this diff followed by running ant should get you a jffi binary that has debug symbols:

diff --git a/jni/GNUmakefile b/jni/GNUmakefile
index cfe570a..4a8a061 100755
--- a/jni/GNUmakefile
+++ b/jni/GNUmakefile
@@ -61,7 +61,7 @@ LIBNAME = jffi
 # Compiler/linker flags from:
 #   http://weblogs.java.net/blog/kellyohair/archive/2006/01/compilation_of_1.html
 JFLAGS = -fno-omit-frame-pointer -fno-strict-aliasing -DNDEBUG
-OFLAGS = -O2 $(JFLAGS)
+OFLAGS = -Og -g $(JFLAGS)
 
 # MacOS headers aren't completely warning free, so turn them off
 WERROR = -Werror

We have no leads as to why this particular example crashes inside jffi only on some platforms.

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

No branches or pull requests

2 participants