Re: GObject get_property method copying string



Stephan,

On Thu, Aug 18, 2011 at 4:43 PM, Stefan Kost <ensonic hora-obscura de> wrote:

On 08/11/11 13:52, Tiago Katcipis wrote:
>
>
> Hi,
>
> I'm developing a library using GObject and I'm having trouble creating
> a string property that does not makes a copy when g_object_get is
> called. I used g_value_set_static_string to avoid the string from
> being copied, but doing some debug it seems that g_object_get is
> copying my string anyway.
>
> That is odd, because i see a lot of code using g_value_set_string on
> get_property, and that would produce a completely useless additional
> copy of the string (since g_object_get will copy it anyway).
where is the extra copy being made in glib?

I'm not sure, but for example, i have the following get_property function:

static void
gsip_codec_info_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    GSipCodecInfo * codec = GSIP_CODEC_INFO(object);

    switch (prop_id)
    {
  
    case PROP_CODEC_INFO_PAYLOAD:
            return g_value_set_static_string (value, codec->payload);
        break;
    
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

I expected that the returned string would be the same as codec->payload, but on my tests, it is not, what i get when i call:

gchar * payload = NULL;

g_object_get (codec_info, "payload", &payload, NULL);

is a copy of codec->payload. I confirmed that freeing payload and unrefing codec_info, it would result on a double free, but it actually works (also printing the address of the pointers obviously shows that is a copy).

Doing more debug, i confirmed that g_value_set_static_string is not copying the string (GValue holds the same gchar *), but something between there and my g_object_get call is copying it.

Best regards,
Katcipis


Stefan
>
> Sorry to bother with such a stupid question but the documentation just
> says:
>
> "In general, a copy is made of the property contents and the caller is
> responsible for freeing the memory in the appropriate manner for the
> type, for instance by calling |g_free()|
> <http://developer.gnome.org/glib/stable/glib/glib-Memory-Allocation.html#g-free>
> or |g_object_unref()|
> <http://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref>.
> "
>
> What would be "in general" ? Do i have some control of it ? (i was
> unable to find any) Am i understanding something wrong ?
>
> Best regards,
> Katcipis
>
>
> _______________________________________________
> gnome-devel-list mailing list
> gnome-devel-list gnome org
> http://mail.gnome.org/mailman/listinfo/gnome-devel-list

_______________________________________________
gnome-devel-list mailing list
gnome-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gnome-devel-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]