vmagnin / gtk-fortran

A GTK / Fortran binding, and its documentation in the Wiki tab.
GNU General Public License v3.0
256 stars 43 forks source link

Issue in converting types for g_variant_new_tuple #289

Closed jnez137 closed 3 weeks ago

jnez137 commented 3 months 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 months 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 2 months 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

vmagnin commented 4 weeks ago

@jnez137 you can have a look in my gtk4-vmagnin development branch at this commit: https://github.com/vmagnin/gtk-fortran/commit/2f7631c9f7cfe65c87792798f0143049d2a78fce

GVariant and GVariantType are now declared as an array of pointers when there are two * in the C declaration. The functions concerned with this change are:

gtk-fortran 4.7.0 should be released in a few weeks.

jnez137 commented 4 weeks ago

HI Vincent,

Thanks! Yes I was able to build this commit and it fixes the issue I had.

Thanks, Jeremy

On Tue, Oct 29, 2024 at 6:43 AM Vincent Magnin @.***> wrote:

@jnez137 https://github.com/jnez137 you can have a look in my gtk4-vmagnin development branch at this commit: 2f7631c https://github.com/vmagnin/gtk-fortran/commit/2f7631c9f7cfe65c87792798f0143049d2a78fce

GVariant and GVariantType are now declared as an array of pointers when there are two * in the C declaration. The functions concerned with this change are:

  • gtk_widget_class_query_action
  • g_settings_backend_flatten_tree
  • g_action_group_query_action
  • g_menu_attribute_iter_get_next
  • g_action_parse_detailed_name
  • g_variant_type_new_tuple
  • g_variant_new_tuple
  • g_variant_new_array

gtk-fortran 4.7.0 should be released in a few weeks.

— Reply to this email directly, view it on GitHub https://github.com/vmagnin/gtk-fortran/issues/289#issuecomment-2444273357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVX64HM5MBMGN67XIWD2L3Z56GIJAVCNFSM6AAAAABMPCE5YKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINBUGI3TGMZVG4 . You are receiving this because you were mentioned.Message ID: @.***>

vmagnin commented 3 weeks ago

gtk-fortran 4.7.0 released with the fix.