[Newbie] finalize/dispose and get/set_property
- From: Laurent JALLET <jallet irit fr>
- To: gtk-app-devel-list gnome org
- Subject: [Newbie] finalize/dispose and get/set_property
- Date: Thu, 28 Apr 2005 12:13:44 +0200
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);
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 ?
//
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 ));
//
// 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 ***
2.
I've got another question concerning get/set_property() :
I've got a public function to acces mPosition
what should it be ?
Vector3 * camera_getPosition(Camera *a)
{
return a->priv->mPosition;
}
or
Vector3 * camera_getPosition(Camera *a)
{
Vector3* pos;
g_object_get (G_OBJECT (a), "position", &pos, NULL);
return pos;
}
or none of these ?
just :
g_object_get (G_OBJECT (a), "position", &pos, NULL);
but in this case
should I do :
{
Vector3 * pos;
g_object_ref(pos);
g_object_get (G_OBJECT (a), "position", &pos, NULL);
// do things
g_object_unref(pos);
}
?
with
// is it correct ?
static void
camera_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
[...]
case CAMERA_POSITION: {
g_value_set_object (value, self->priv->mPosition);
break;
[...]
}
// is it correct ?
static void
camera_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
Camera *self = (Camera *) object;
Vector3 *vtmp;
switch (property_id) {
case CAMERA_POSITION: {
vtmp = (Vector3 *) g_value_get_object (value);
camera_setPosition(self, vtmp);
}
break;
[...]
}
}
3.
and what should I do when I'd like to translate the camera ? =>
void camera_localTranslate(Camera *a, Vector3 * t)
// I've writen that using a->priv->mPosition directly (and other members..)
Is it correct to change directly mPosition without using
set/get_property or camera_getPosition() / camera_setPosition() ?
I know this questions must look stupid, and there's too much code, but
I'm quite lost using GObject (and in fact, C programing...).
Thank you by advance
L.J.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]