jwharm/java-gi

Segfault when clearing entry

Closed this issue · 1 comments

This is probably a bug in GTK and not in this library, but I found it here and know nothing about C, so I am reporting it here.
When setting the content of a org.gnome.gtk.Entry, I usually use the setText convenience method provided by Editable. This works for strings containing one or more characters, but causes a segfault for empty strings ("") or null. While the latter is expected, the former seems weird.
I know I can work around this using the buffer obtainable via entry.getBuffer(), but that removes the point of having a convenience method in the first place.

Code (using GTK4 4.12.0, GLib 2.76.4, java-gi 0.6.1 and OpenJDK 20.0.2)
package io.gitlab.jfronny;

import org.gnome.gtk.*;
import org.gnome.gio.ApplicationFlags;

public class ExampleApp {
    public static void main(String[] args) {
        new ExampleApp().run(args);
    }

    private final Application app;
    public ExampleApp() {
        app = new Application("io.gitlab.jfronny.ExampleApp", ApplicationFlags.DEFAULT_FLAGS);
        app.onActivate(this::activate);
    }

    public void run(String[] args) {
        app.run(args);
    }

    public void activate() {
        var window = new ApplicationWindow(app);
        window.setTitle("GTK Example");
        window.setDefaultSize(300, 200);

        var box = new Box(Orientation.VERTICAL, 0);
        box.setHalign(Align.CENTER);
        box.setValign(Align.CENTER);

        var entry = new Entry();
        entry.setText("This is some text");
        box.append(entry);

        var button1 = Button.newWithLabel("Test Button 1"); // This works
        button1.onClicked(() -> entry.setText("This is different text"));
        box.append(button1);

        var button2 = Button.newWithLabel("Test Button 2"); // This doesn't work
        button2.onClicked(() -> entry.setText(null));
        box.append(button2);

        var button3 = Button.newWithLabel("Test Button 3"); // This doesn't work
        button3.onClicked(() -> entry.setText(""));
        box.append(button3);

        var button4 = Button.newWithLabel("Test Button 4"); // This works
        button4.onClicked(() -> clear(entry.getBuffer()));
        box.append(button4);

        window.setChild(box);
        window.present();
    }

    private void clear(EntryBuffer buffer) {
        buffer.deleteText(0, buffer.getLength());
    }
}
jwharm commented

There's a bug in java-gi version 0.6.1 when marshaling empty strings. The empty string was marshaled to a null pointer. It is already resolved in the main branch (with this commit).