Re: GObject: Freeing memory of instance members?
On Thu, May 03, 2007 at 08:03:15PM -0300, Alexandre Moreira wrote: Hey Yeti, since you're on the subject of object destruction, could you explain me the difference between the finalize and dispose methods ? I use finalize for everything but I believe I should be using dispose somewhere, it wouldn't exist if it were useless...I guess :) See http://developer.gnome.org/doc/API/2.0/gobject/gobject-memory.html Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
On Thu, May 03, 2007 at 08:16:23PM -0300, Alexandre Moreira wrote: 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. Actually, what you should do is to use a macro from the G_DEFINE_TYPE() family that does these things automaticaly (the parent class variable is named my_type_parent_class). 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); This still holds, except for the name. Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
David Nečas (Yeti) wrote: Actually, what you should do is to use a macro from the G_DEFINE_TYPE() family that does these things automaticaly (the parent class variable is named my_type_parent_class). Wow! A really powerful macro. I actually had to run a 'gcc -E' to convince myself. Here's what it does: Declare the functions: my_type_init my_type_class_init Generate the functions: my_type_class_intern_init (which sets the parent_class) my_type_get_type declare the variable: my_type_parent_class Why hasn't anyone told me of this macro before? Do you also have a macro that cleans my house og does the laundry? Thanks, -Øystein ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
Øystein Johansen wrote: Wow! A really powerful macro. I actually had to run a 'gcc -E' to convince myself. Here's what it does: Declare the functions: my_type_init my_type_class_init Hello?! Wait a minute The macro (G_DEFINE_TYPE) prototypes the instance init function to: static void my_thing_init( MyThing *self); (line 358 in my gtype.h) however API reference for GInstanceInitFunc() saysthe arguments should be: (GTypeInstance *instance, gpointer g_class); http://developer.gnome.org/doc/API/2.0/gobject/gobject-Type-Information.html#GInstanceInitFunc I also get a error when compiling. error: conflicting types for 'my_thing_init' error: previous declaration of 'my_thing_init' was here ... .. . . -Øystein ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
On Sat, May 05, 2007 at 05:45:06PM +0200, ??ystein Johansen wrote: Hello?! Wait a minute The macro (G_DEFINE_TYPE) prototypes the instance init function to: static void my_thing_init( MyThing *self); (line 358 in my gtype.h) however API reference for GInstanceInitFunc() saysthe arguments should be: (GTypeInstance *instance, gpointer g_class); Yes, but almost no-one actually uses the second argument. And since unused trailing arguments can be safely ignored in C, commonly they are not even declared. If you really need the second argument (for instance when a parent class performs some common initialization based on subclass-specified information and therefore it needs to know the actual type being initialized) you cannot use these macros. Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
GObject: Freeing memory of instance members?
Hi, I've just started to use GObject, and I think I understand most of it by now. There's still some questions remaining. Here's one: I have an instance struct that has a pointer to a some data that's dynamically allocated in a method. How can I free this memory when the object is finalzed? Is this done automatically in g_object_unref() (when refcounter is zero) ? -Øystein PS: I see a pointer to a func in the info struct called base_finalize and another called class_finalize, but these seems to be used for cleaning up when I have base_init and class_init. ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
On Thu, May 03, 2007 at 09:51:05PM +0200, ?ystein Johansen wrote: I have an instance struct that has a pointer to a some data that's dynamically allocated in a method. How can I free this memory when the object is finalzed? Is this done automatically in g_object_unref() (when refcounter is zero) ? When the reference count reaches zero and the object is finalized, its finalize() method is called. And that's where you should free any possibly allocated memory. Remember to chain up parent class finalize() method in your finalize(). I see a pointer to a func in the info struct called base_finalize and another called class_finalize, but these seems to be used for cleaning up when I have base_init and class_init. Ignore these, they are related to class initialization and finalization. Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
David Nečas (Yeti) wrote: On Thu, May 03, 2007 at 09:51:05PM +0200, ?ystein Johansen wrote: I have an instance struct that has a pointer to a some data that's dynamically allocated in a method. How can I free this memory when the object is finalzed? Is this done automatically in g_object_unref() (when refcounter is zero) ? When the reference count reaches zero and the object is finalized, its finalize() method is called. And that's where you should free any possibly allocated memory. Aha, but how/where to I set the pointer to the finalize() function? Remember to chain up parent class finalize() method in your finalize(). I will! :-) -Øystein ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
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. Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
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? Thanks, -Øystein ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
On 5/3/07, David Nečas (Yeti) [EMAIL PROTECTED] 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. Hey Yeti, since you're on the subject of object destruction, could you explain me the difference between the finalize and dispose methods ? I use finalize for everything but I believe I should be using dispose somewhere, it wouldn't exist if it were useless...I guess :) Thanks, Alexandre Moreira. Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: GObject: Freeing memory of instance members?
On 5/3/07, Øystein Johansen [EMAIL PROTECTED] 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 ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list