diamondburned/gotk4

Crash when using an action callback with its args

Closed this issue · 4 comments

sqp commented

I'm trying the new menu features and wanted to register a callback with its args when I encountered a strange bug

	isMaximized := appInfo.Win.IsMaximized()
	action_fullscreen := gio.NewSimpleActionStateful("fullscreen", nil, glib.NewVariantBoolean(isMaximized))
	action_fullscreen.Connect("change-state", func(action *gio.SimpleAction, value *glib.Variant) {
		fmt.Println("menu action_fullscreen")
	})
	appInfo.Win.AddAction(action_fullscreen)

When I click on the related menu button, it panics when trying to recast the object to the same(?) type:

panic: reflect.Value.Convert: value of type *glib.Variant cannot be converted to type *glib.Variant [recovered]
panic: closure error: unexpected panic caught: reflect.Value.Convert: value of type *glib.Variant cannot be converted to type *glib.Variant

The line that fail (glib.go:208) :

args = append(args, reflect.ValueOf(val).Convert(fsType.In(i)))

I added some output to see what's happening

println(reflect.TypeOf(val).String(), fsType.In(i).String(), reflect.TypeOf(val) == fsType.In(i))

that prints just before the crash.

*gio.SimpleAction *gio.SimpleAction true
*glib.Variant *glib.Variant false

The *gio.SimpleAction is nicely recognized and recasted, but there seem to be some mystery with the *glib.Variant
If needed, here's the C code I'm using as example
and my imports :

	"github.com/diamondburned/gotk4/pkg/glib/v2"
	"github.com/diamondburned/gotk4/pkg/gio/v2"
	"github.com/diamondburned/gotk4/pkg/gtk/v4"

(damn 24 min to recompile the whole glib+gtk on my old i7 proc)

As far as glib.Variant goes, you need to import
github.com/diamondburned/gotk4/pkg/core/glib for it, because the GVariant
code was primarily hand-written.

The most straightforward solution to fix this confusion would be to have a
generation edge case, but currently, the library only allows ignoring the type
during both generation and resolving, and edge cases to that must be hard-coded
in the type resolver.

As a sidenote, just compiling GTK4 by itself should be faster, since GTK3 takes
longer to compile. You could also try applying certain Go patches listed in
.nix/overlay.nix that tries to multithread the Cgo build process.

As of commit 579eec99a, callbacks should be using pkg/glib/v2.Variant instead of pkg/core/glib.Variant.

I'll be closing this issue since it's kind of fixed.

Reopening since the commit didn't actually work. I'll fix it in a minute.

Fixed in b63160e.