gtk-fortran icon indicating copy to clipboard operation
gtk-fortran copied to clipboard

Issue in converting types for g_variant_new_tuple

Open jnez137 opened this issue 6 months ago • 2 comments

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 <Meta>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 <Meta>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.

jnez137 avatar Aug 13 '24 21:08 jnez137