Pointers in GEqualFunc

GListStore offers g_list_store_find_with_equal_func() to find an element inside the list store. One parameter of that function call is GEqualFunc equal_func, which compares each element in the store with a criterion to find the desired element.

The signature for a GEqualFunc is as follows:

gboolean
(* GEqualFunc) (
gconstpointer a,
gconstpointer b
)

Is it true that a and b must point to objects of the same type? For example, must both a and b point to a GValue? I ask because I’ve tried passing objects of different types, such as a sublcass of GObject and GINT_TO_POINTER, but I get a runtime error when the callback runs.

A GListModel implementation, such as GListStore, can only store GObject instances of the same type, which means both a and b arguments of the GEqualFunc are a pointer to a GObject, and have the same GType.

You’re not supposed to pass the pointers yourself: you are given two pointers, and it’s up to you to compare them for equality. For instance, if we assume that you created a model such as:

GListStore *store = g_list_store_new (GTK_TYPE_STRING_OBJECT);

Then you would write an equality function like this one:

static gboolean
compare_string_objects (gconstpointer a,
                        gconstpointer b)
{
  GtkStringObject *str_a = GTK_STRING_OBJECT (a);
  GtkStringObject *str_b = GTK_STRING_OBJECT (b);

  return g_str_equal (gtk_string_object_get_string (str_a),
                      gtk_string_object_get_string (str_b));
}

Thank you for confirming.