vmagnin / gtk-fortran

A GTK / Fortran binding. The documentation is in the Wiki tab.
GNU General Public License v3.0
251 stars 43 forks source link

Issue in converting types for g_variant_new_tuple #289

Open jnez137 opened 1 month ago

jnez137 commented 1 month ago

Describe the bug This is very similar to closed issue https://github.com/vmagnin/gtk-fortran/issues/277 When I try to use g_variant_type_new_tuple it crashes with the following error with the current binding.

(<unknown>:88500): GLib-CRITICAL **: 09:24:54.291: g_variant_ref_sink: assertion '!g_atomic_ref_count_compare (&value->ref_count, 0)' failed

Expected behavior I changed the binding to accept a pointer array, recompiled, and it is working as expected.

!GVariant * g_variant_new_tuple (GVariant * const *children, gsize n_children);
function g_variant_new_tuple(children, n_children) bind(c)
  import :: c_ptr, c_size_t
  implicit none
  type(c_ptr) :: g_variant_new_tuple
  !Original
  !type(c_ptr), value :: children
  ! Changed line
  type(c_ptr), dimension(*) :: children
  !
  integer(c_size_t), value :: n_children
end function

To Reproduce I was using MacOS. Here is a snippet of code that will reproduce the error, edited from a sub that I use to add Mac key bindings for clipboard cut/copy/paste

  type(c_ptr), intent(inout) :: textView
  type(c_ptr) :: scSelectAll
  type(c_ptr :: ptr_selectAll
  integer(kind=c_int64_t) :: nChild = 1

  scSelectAll = gtk_shortcut_new( &
  & gtk_shortcut_trigger_parse_string("<Meta>a"//c_null_char), &
  & gtk_signal_action_new("select-all"//c_null_char))

  ptr_selectAll = g_variant_new_boolean(1_c_int)

  call gtk_shortcut_set_arguments(scSelectAll, &
  & g_variant_new_tuple(ptr_selectAll, nChild))

  scControl = gtk_shortcut_controller_new()
  call gtk_shortcut_controller_add_shortcut(scControl, scSelectAll)

  call gtk_widget_add_controller(textView, scControl)

If you were to run an application with a textView modified in this way, and click on the textView and use the a shortcut key, you will get the error above.

With the fix I proposed above, the code snippet has to be modified slightly to create a null terminated pointer string

  type(c_ptr), dimension(2) :: ptr_selectAll
...
  ptr_selectAll(1) = g_variant_new_boolean(1_c_int)
  ptr_selectAll(2) = c_null_ptr

Making this change results in no crash and the selected behavior (the a key binding selects all the text in the textView).

Your system: MacOS gtk4 gtk-fortran.

Additional context This seems similar to issue #277 where the type conversion is not clear. I don't know if there is a more general way to catch these. I also didn't see any evidence of this routine being tested in your examples so I am guessing it has never worked.

vmagnin commented 3 weeks ago

Thanks @jnez137 for pointing this problem.

Indeed, following the issue #277 I had written:

        if ("char" in c_type) and (not isReturned):
            if declaration.count('*') >= 2:
                # An array of C strings:
                return "type(c_ptr), dimension(*)", "c_ptr"

I will have to investigate if we can follow the same rule for the GVariant type.

vmagnin commented 1 week ago

https://docs.gtk.org/glib/ctor.Variant.new_tuple.html https://docs.gtk.org/glib/ctor.Variant.new_array.html https://docs.gtk.org/glib/ctor.VariantType.new_tuple.html

Concerns GVariant and GVariantType