g_object_get always return a string duplicate even when G_VALUE_NOCOPY_CONTENTS is set.
Hello!
Whenever "g_object_get" is called to get a String (gchar*) property from a GObject, it's always a duplicate which is returned, not the actual pointer to the same memory location.
Simple proof of concept in attachment : "poc-gobject.c" poc-gobject.c
Using "g_value_set_static_string" which explicitly set "G_VALUE_NOCOPY_CONTENTS" has no effect (inside "my_object_get_property").
In this example 'name1' and 'name2' point to the same memory location, but not 'name3' (because it's a copy) :
gchar *name1 = "HELLO";
MyObject *obj = my_object_new();
my_object_set_name(obj, name1);
const gchar *name2 = my_object_get_name(obj);
gchar *name3;
g_object_get(G_OBJECT(obj), "name", &name3, NULL);
This steps may be the cause :
-
g_object_get_valist
callG_VALUE_LCOPY
in https://gitlab.gnome.org/GNOME/glib/-/blob/main/gobject/gobject.c#L2559 :
G_VALUE_LCOPY (&value, var_args, 0, &error);
-
From
G_VALUE_LCOPY
definition : https://gitlab.gnome.org/GNOME/glib/-/blob/main/gobject/gvaluecollector.h#L209 The 3rd argument is flags -
Which then call
value_lcopy_string
from https://gitlab.gnome.org/GNOME/glib/-/blob/main/gobject/gvaluetypes.c#L299
As as you can see in 1), the "flag" argument is set to 0, so instead "value_lcopy_string" will duplicate the string because it cannot enter the condition :
collect_flags & G_VALUE_NOCOPY_CONTENTS"
if (!value->data[0].v_pointer)
*string_p = NULL;
else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
*string_p = value->data[0].v_pointer;
else
*string_p = g_strdup (value->data[0].v_pointer);
The solution is to modify the call to "G_VALUE_LCOPY" to actually use the GValue flags if one is set.
- GLib Version : glib2-2.66.8-1.fc33.x86_64
- Operating system : Fedora 33 x86_64
- GCC Version : 10.3.1 20210422 (Red Hat 10.3.1-1) (GCC)