Re: Initialisation/Finalisation
- From: iain <iain prettypeople org>
- To: John Darrington <jmd computer org>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: Initialisation/Finalisation
- Date: 05 Nov 2002 10:29:51 +0000
On Mon, 2002-11-04 at 12:18, John Darrington wrote:
My Questions are:
1. The Finalize func undoes the Init func. Fair enough. But why is
there no GInstanceFinalizeFunc? See example below:
All objects I've ever written have had NULL for everything except the
GClassInitFunc and GInstanceInitFunc.
Actually I think I had one that had NULL for GInstanceInitFunc too once.
So basically, you don't need to worry about them.
I have a gobject, whose _new function looks like :
GObject*
pgc_new(const gchar *c)
{
PGC *pg;
pg = PGC(g_object_new(TYPE_PGC,0));
g_assert(pg);
pg->info = g_strdup(c);
return G_OBJECT(pg);
}
Now the doco for g_strdup says that the pointer returned must be freed
when no longer needed. But how and when can I do that ?? It needs to
be freed when the object is destroyed, but there is no
GInstanceFinalizeFunc which (to me) would seem the logical thing to
have.
Override either the dispose or the finalize methods of the GObjectClass
like so.
static void
finalize (GObject *object)
{
PGC *pg;
pg = PGC(object);
g_free (pg->info);
/* Other things here */
/* Chain up to the parents finalize */
G_OBJECT_CLASS (parent_class)->finalize (object);
}
/* And override the finalize method here */
static void
class_init (PGCClass *klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = finalize;
}
Now whenever the object is destroyed, the memory will be freed.
iain
--
"This way of life is so devised,
To snuff out the mind that moves"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]