Check an GObject



Hi,

I develop an application which using GObject and specific derived objects (in GTK 2.4 or 3.x).

A module receives a pointer. It should be a GObject or derived but i'm not sure (I'm not the sender). So, to avoid bug / corruption / crash, i would check the quality of this pointer : GObject or not ?
How can i make this ?

Example :

************************************
#include <stdio.h>
#include <gtk/gtk.h>

int main(int argc, char **argv)
{
    gboolean b;
    GtkWidget *widget;
    gint i = 10;
    gchar *str = "bob";

    g_message ("0");
    g_type_init ();

    g_message ("1");
    widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    b = G_IS_OBJECT (widget);
    g_message ("A : %d", b);    /* OK, true */

    g_message ("2");
    b = G_IS_OBJECT (i);        /* KO : crash */
    g_message ("B : %d", b);

    g_message ("3");
    b = G_IS_OBJECT (str);        /* KO : crash */
    g_message ("C : %d", b);

    return 0;
}

************************************

Compilation : gcc `pkg-config --cflags --libs gtk+-3.0` -Wall -g test.c -o test

output :

************************************
** Message: 0
** Message: 1
** Message: A : 1
** Message: 2
Segmentation fault

************************************

Same thing with G_TYPE_IS_OBJECT.

Regards,


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