Re: [Newbie] finalize/dispose and get/set_property



Hi Laurent,

please see inline comments.
hi all,

I'm using GObject to do "Object Programing in C", I'm not using GTK.
I search in the mailing-list archive and I didn't get what I'd like.

I've got pragmatic questions, quite "simple" (although I don't understand), in order to have basics for doing deeper things :

1.
I know it's a redundant question but I still don't understand how to use finalize() and dispose(). I understand that dispose() is called when a g_object_unref() is called and that finalize() is called when the last g_object_unref() is called.

I've got a GObject Camera, with :

struct _CameraPrivate {
 Vector3 * mPosition;
 // and other members   gboolean dispose_has_run;
};

Vector3 is a GObject, with no private struct (<- no need)
struct _Vector3 {
 GObject parent;
 gfloat x, y, z;

 gboolean dispose_has_run;
};


I don't know what to do when I unref a camera : g_object_unref (G_OBJECT (camera));
I'd like to unref camera->self->mPosition, but when? and How ?
And when do I free mPosition (g_free (self->priv->mPosition)) ?
(I don't use signal)

I' tried to unref(mPosition) in the camera_dispose(), but i've got an error.


Here is my code :


static void
camera_instance_init (GTypeInstance   *instance,
                        gpointer         g_class)
{
 Camera *self = (Camera *)instance;
        self->priv = g_new (CameraPrivate, 1);

 self->priv->mPosition =  g_new (Vector3, 1);
if the Vector3 thing is a GObject this should be
  self->priv->mPosition = g_object_new(<the Vector3 type>,NULL);

 vector3_set(self->priv->mPosition, -1.15, 32.0, 43.75);   // or whatever

 self->priv->dispose_has_run = FALSE;
}

//
// what should I do ?
// replace mPosition by v ? ( => unref(mPosition) & ref(v)  )
// or like I do just change the members x, y and z ?
//
here I recommend to use GObject properties. Reread the gobjet tutorial and watch out of g_object_class_install_property() and the _set_property(), _get_property() methods

void camera_setPosition(Camera *a, Vector3 * v)
{
 a->priv->mPosition->x = v->x;
 a->priv->mPosition->y = v->y;
 a->priv->mPosition->z = v->z;
}


static void
camera_dispose (GObject *obj)
{
 Camera *self = (Camera *)obj;

 if (self->priv->dispose_has_run) {
   // If dispose did already run, return.
   return;
 }
        // Make sure dispose does not run twice
 self->priv->dispose_has_run = TRUE;

 g_print("|| camera dispose ||\n");

   // In dispose, you are supposed to free all types referenced from this
 // object which might themselves hold a reference to self. Generally,
 // the most simple solution is to unref all members on which you own a
 // reference.

//
// that why I try to unref mPosition
//
 g_object_unref (G_OBJECT ( self->priv->mPosition ));
//
if you turn the g_new() in init() into a g_object_new() then this will work.
As a rule of the thumb, release all references in dispose() and release all memory in finalize().

 // Chain up to the parent class
 G_OBJECT_CLASS (parent_class)->dispose (obj);
}

static void
camera_finalize (GObject *obj)
{
 Camera *self = (Camera *)obj;

 g_print("*** camera finalize ***\n");

//
// what sould I do ? g_free or g_object_unref  (mPosition) ?
//  g_free (self->priv->mPosition);
//  g_object_unref (G_OBJECT (self->priv->mPosition) );
//

 g_free (self->priv);

 /* Chain up to the parent class */
 G_OBJECT_CLASS (parent_class)->finalize (obj);
}



result =>

|| camera dispose ||

(process:15709): GLib-GObject-WARNING **: invalid unclassed pointer in cast to `GObject'

(process:15709): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed
*** camera finalize ***



Stefan



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