After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 679481 - GLib.Object.get_class () doesn't work
GLib.Object.get_class () doesn't work
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Bindings: GLib
0.17.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
: 638208 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2012-07-05 23:50 UTC by Richard Schwarting
Modified: 2014-06-27 04:17 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Richard Schwarting 2012-07-05 23:50:21 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.
Comment 1 Evan Nemerson 2014-06-12 03:34:54 UTC
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 ();
Comment 2 Evan Nemerson 2014-06-27 04:17:32 UTC
*** Bug 638208 has been marked as a duplicate of this bug. ***