Some questions about Gobject based classes



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm working on the gobject class model and wanted to implement a (trivial) class based on gobject, but I still have some
questions:

I'm working with the book "GNOME 2.0, Das Entwicklerhandbuch" (Developers manual) by Matthias Warkus (I don't know if
this book has an english translation) and I've been also reading the GObject Reference Manual as well. I understand
almost everything but there are some issues about freeing memory of objects insied a class structure that I don't really
 understand at all.

I have a simple class called GtestMedium:

typedef struct _GtestMedium GtestMedium;
typedef struct _GtestMediumClass GtestMediumClass;


struct _GtestMedium {
    GObject  g_obj;

    GString  *place;
    GString  *title;
    /* properties */
    guint    archiv_nr;
    gboolean orig_pack;
};


struct _GtestMediumClass {
    GObjectClass g_obj_class;

    /* signals */
    void (*unpacking)(GtestMedium *medium);
    void (*dispose)(GtestMedium *medium,
                    gboolean    definitely);
};


archiv_nr & orig_pack are already declared as properties, I can set and get them with g_object_set/get without problems.
I want to declare 'place' and 'title' as properties as well:

g_object_set(medium, "title", "ABC...", NULL);

'place' and 'title' are GString objects and have to be freed with g_string_free. I don't know where I have to do that.
gtest_medium_get_type looks like:

GType gtest_medium_get_type(void)
{
    static GType medium_type = 0;

    if(medium_type == 0)
    {
        const GTypeInfo medium_info =
        {
            sizeof(GtestMediumClass),    /* size of MediumClass */
            NULL,                        /* base init */
            NULL,                        /* base finalize */
            (GClassInitFunc)
                gtest_medium_class_init, /* class init */
            NULL,                        /* class finalize */
            NULL,                        /* class data */
            sizeof(GtestMedium),         /* size of Medium */
            16,                          /* 16 pre allocs */
            NULL                         /* instance init */
            /* skip value_table */
        };

        medium_type = g_type_register_static(G_TYPE_OBJECT,
                "GtestMedium",
                &medium_info,
                0);

        g_printf("DEBUG: Hello GOBJECT World %d\n", medium_type);
    }

    return medium_type;
}


I've written a trivial gtest_medium_class_finalize function

static void gtest_medium_class_finalize(GtestMediumClass *klass)
{
    g_printf("bye bye\n");
}

but if change the class finalize member (of GTypeInfo) from NULL to (GClassFinalizeFunc) gtest_medium_class_finalize
then nothing works afterwards:

$ ./test
(process:15609): GLib-GObject-WARNING **: class finalizer specified for static type `GtestMedium'
DEBUG: Hello GOBJECT World 0
...

So, where can I free the memory of 'place' and 'title'?

* * *

Both in my book and in the GObject Reference Manual the class_init function is declared as
  void function_name(Klassname *ptr);
although the GClassInitFunc expects two pointers instead of one. Why does this work? I had once something similar: I had
a function pointer that took 4 parameters and a couple of functions that could be assigned to but the choice could be
only be made on runtime. After some time one of those functions changed and expcted 5 instead of 4 parameters. I didn't
realize that at first but then my applications crashed and valgrind always told me about branches that were taken on
uninitialized variables, after some hours spent with debuggin I realized that the stacked was corruped beacause of this
function that execped 5 parameter but got only 4 (through the casted function pointer). This is more or less the same
case, that's why I ask myself: why does it work here?

thank
regards
Pablo


- --
Pablo Yánez Trujillo
http://klingsor.informatik.uni-freiburg.de/
My public key: http://klingsor.informatik.uni-freiburg.de/gpg/supertux.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkkQ6Y4ACgkQDzf8xo+0xRWzsgCcCZ8KrGEKvPd5F998pzFmfqz0
z7QAoJvvdaTW9PbRPNvoEM3ymOzVx5ER
=2WYN
-----END PGP SIGNATURE-----


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