Re: Initialisation/Finalisation



On Wed, 2002-11-06 at 10:38, John Darrington wrote:

1.   /* And override the finalize method here */
     static void
     class_init (PGCClass *klass)

     I take it your typo was in the comment rather than the code?


What typo?
The klass? I just write it that qay out of habit and the c++ compilers
that don't like variables named class.

2.    static void   finalize (GObject *object)

      This is a CLASS finalize function, yes?  Why therefore does it
      take an OBJECT as it's argument?  This seems especially
      inconsistent, when the class_init function takes a CLASS.


No, it's an object finalize function. Thats why it takes an object.

3.    Why is it necessary to bother about the 

       object_class->finalize = finalize;

      line, when there is a field in the Typeinfo structure to specify
      the finalise function.  Is it necessary to specify it twice?  If
      so, it seems a bit precarious to me.


because the one in the typeinfo struct is the class finalize function.
You don't need to have one. Like I said NULL works fine.
The one in class_init is overriding the object finalize function.

      By reading other examples in the docs, I take it that
      parent_class is a static (file scoped) variable, (not a member
      of any struct).  It seems odd to me that such a loose variable
      should be necessary, when everything else is nicely encapsulated
      either in the class or object variables.


*shrug*
Just the way its done I guess.

1.    When I put the code in that you suggested I get the following
      run time messages:

 (process:9485): GLib-GObject-WARNING **: class finalizer specified for static type `PGClient'

 (process:9485): GLib-GObject-WARNING **: class finalizer specified for static type `PGClient'

 (process:9485): GLib-GObject-CRITICAL **: file gobject.c: line 588 (g_object_new): assertion 
`G_TYPE_IS_OBJECT (object_type)' failed

 (process:9485): GLib-GObject-WARNING **: invalid cast from (NULL) pointer to `<invalid>'

 ** ERROR **: file pgclient.c: line 68 (pgclient_new): assertion
    failed: (pg)


2.    So I replaced the ClassFinaliseFunc pointer in the typeinfo
      struct (see point 3. above) to see what happens.  This time,
      there's no run time errors, but the finalize func never gets
      called (as evidenced by a g_print() diagnostic that I put in.


Iain's quick object
Hopefully it'll make things clearer than either me describing stuff, or
giving random snippets of code.

typedef struct _Obj {
        GObject object_parent;
        char *data; /* This needs freed on destruction */
} Obj;

typedef struct _ObjClass {
        GObjectClass parent_class;
} ObjClass;

static GObjectClass *parent_class;

GType
obj_get_type (void)
{
static GType type = 0;

if (type == 0) {
static GTypeInfo info = {
        sizeof (ObjClass), NULL, NULL,
        (GClassInitFunc) class_init, NULL, NULL,
        sizeof (Obj), 0, (GInstanceInitFunc) init
};

type = g_type_register_static (G_OBJECT_TYPE, "IainsObject", &info, 0);
}

return type;
}

static void
finalize (GObject *object)
{
Obj *obj = (Obj *) object;

g_print ("We're destroying the object");
g_free (obj->data);

/* Chain to parent's finalize */
parent_class->finalize (object);
}

static void
class_init (ObjClass *klass)
{
GObjectClass *object_class;

object_class = G_OBJECT_CLASS (klass);

/* Override the object's finalize method with our own */
object_class->finalize = finalize;

/* Store the parent pointer for later */
parent_class = g_type_class_peek_parent (klass);
}

static void
init (Obj *obj)
{
obj->data = g_strdup ("This string needs to be freed when the object is
finalized");
}

Obj *
obj_new (void)
{
return g_object_new (obj_get_type, NULL);
}

Create a new obj with obj_new (), and when the obj is finalized it will
delete the memory used by obj->data and then call the parent class's
instance finalize function.

So, you don't need to worry about the Finalize members of the typeinfo
struct, and thats probably why you started getting warnings about them.

I've been writing GObjects (and GtkObjects) for 4 years now, and I've
never written an object that has anything other than a class init func
and an instance init func.

iain
-- 
"Things like Destiny's Child are just repressed sexuality. It happens
with boys and men too. I feel embarrassed for them when I listen to
Stereophonics or Coldplay. That's not music for men." -- Alec Empire




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