GNOME Bugzilla – Bug 679481
GLib.Object.get_class () doesn't work
Last modified: 2014-06-27 04:17:32 UTC
Trying to do something simple like this: --- public static int main (string[] args) { Object obj = new Object (); ObjectClass cclass = obj.get_class (); return 0; } --- Generates buggy C code with compiler warnings like this: --- /tmp/type.vala.c: In function '_g_type_class_ref0': /tmp/type.vala.c:19:2: warning: passing argument 1 of 'g_type_class_ref' makes integer from pointer without a cast [enabled by default] In file included from /usr/include/glib-2.0/gobject/gobject.h:26:0, from /usr/include/glib-2.0/gobject/gbinding.h:31, from /usr/include/glib-2.0/glib-object.h:25, from /tmp/type.vala.c:6: /usr/include/glib-2.0/gobject/gtype.h:678:23: note: expected 'GType' but argument is of type 'gpointer' Compilation succeeded - 1 warning(s) --- With C code like this: --- obj = _tmp2_; _tmp3_ = G_OBJECT_GET_CLASS (obj); _tmp4_ = _g_type_class_ref0 (_tmp3_); cclass = _tmp4_; --- Where _g_type_class_ref0 is vala-generated: --- static gpointer _g_type_class_ref0 (gpointer self) { return self ? g_type_class_ref (self) : NULL; } --- which results in a runtime error like this --- (process:3119): GLib-GObject-WARNING **: cannot retrieve class for invalid (unclassed) type `(null)' --- So the problem seems to be that we get a GObjectClass in _tmp3_ and then we treat it like a GType in _g_type_class_ref0. The wonderful nemequ in #vala has suggested as a work-around saving the GLib.ObjectClass into an unowned variable for now.
commit d8fb7fd3c297f91b5613b18b1d3fa1d76260ea82 Author: Evan Nemerson <evan@nemerson.com> Date: Wed Jun 11 20:31:08 2014 -0700 gobject-2.0: make TypeClass non-reference-counted The ref function was always broken and would generate invalid C, so this should be safe. Fixes bug 679481. You still will not be able to assign Object.get_class to an owned variable, but at least this way you'll get a sane error instead of invalid C. The only work-around I can think of if you really need an owned value is something like this: ObjectClass cclass = (GLib.ObjectClass) obj.get_class ().get_type ().class_ref ();
*** Bug 638208 has been marked as a duplicate of this bug. ***