Re: [Vala] GObject.get() + struct in Vala is problematic



GLib.Object.get() actually is g_object_get() in C.

ClutterColor is registered as a boxed type in GObject's term.

Returns boxed type from g_object_get() in GObject type system will make a
copy with g_boxed_copy() first, so, what you get is a pointer instead of
simple value (pass by value).

Here is the sample code in C
ClutterColor * bg;
ClutterActor * actor = clutter_rectangle();
g_object_get(actor, "background-color", & bg, NULL);
g_message("%d, %d, %d, %d", bg->red, bg->green, bg->blue, bg->alpha);
clutter_color_free(bg);
g_object_unref(actor);

$ gcc -o test test.c $(pkg-config --libs --cflags clutter-1.0) && ./test
** Message: 0, 0, 0, 0

Because GObject type system return a address of a copied ClutterColor, the
third parameter must be a pointer to ClutterColor pointer (ColorColor **)
instead of ClutterColor pointer (ClutterColor *).

The above sample code in Vala
Clutter.Color bg;
Clutter.Actor actor = new Clutter.Rectangle();
actor.get("background-color", out bg);
message("%d, %d, %d, %d", bg.red, bg.green, bg.blue, bg.alpha);

$ valac -o test test.vala --pkg=clutter-1.0 && ./test
** Message: test.vala:8: 40, 129, 22, 8

Generated C code looks like
ClutterColor bg;
ClutterActor * actor = clutter_rectangle();
g_object_get(actor, "background-color", & bg, NULL);
g_object_unref(actor);

The value filled into bg is not the color value but address of copied
ClutterColor.

If we declare bg as a ClutterColor pointer
Clutter.Color * bg;
Clutter.Actor actor = new Clutter.Rectangle();
actor.get("background-color", out bg);
message("%d, %d, %d, %d", bg->red, bg->green, bg->blue, bg->alpha);

$ valac -o test test.vala --pkg=clutter-1.0 && ./test
** Message: test.vala:8: 0, 0, 0, 0

It is the expected result (but I don't know how to free the returned bg).

Derek Dai


On Sat, Sep 15, 2012 at 3:53 PM, <vala-list-request gnome org> wrote:

Re: [Vala] GObject.get() + struct in Vala is problematic


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