Re: GObject: Freeing memory of instance members?



On 5/3/07, Øystein Johansen <oystein gnubg org> wrote:
David Nečas (Yeti) wrote:
> On Thu, May 03, 2007 at 10:40:17PM +0200, ??ystein Johansen wrote:
>> Aha, but how/where to I set the pointer to the finalize() function?
>
> In my_class_init() where you override all other methods.
> See (almost) any Gtk+ widget for examples.

Aha again! I guess I can free private members in the same method as well?

static void
neural_net_finalize (GObject *object)
{
  NeuralNet *nn;

  g_return_if_fail (IS_NEURAL_NET(object));

  nn = NEURAL_NET(object);

  g_free(nn->priv->weight_h);
  g_free(nn->priv->weight_o);
  g_free(nn->priv->bias_h);
  g_free(nn->priv->bias_o);
  g_free(nn->priv->filename);
  g_free(nn->priv);

  g_message("This is the finalize method and I've freed the instance
members!\n");

}

static void
neural_net_class_init (NeuralNetClass *klass){
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
        gobject_class->finalize = neural_net_finalize;
        g_type_class_add_private (klass, sizeof (NeuralNetPrivate));
}

I think this looks ok, doesn't it? No, wait a minite.... What about the
parent? This class has GObjectClass as parent, how do I call it's finalize?

At your class_init you should peek (g_type_class_peek_parent, using
your class pointer as parameter) a pointer to your parent class and
keep it in a global storage, accessible to your finalize.

Then, in your finalize method you call the finalize method on this
pointer you just got from peek_parent, something like:

G_OBJECT_CLASS(parent_class)->finalize(pointer_to_the_object_being_finalized);

I hope this helps.
Alexandre Moreira.


Thanks,
-Øystein

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list



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