Hi ! I’m learning to code GTK Applications, and I’m really struggling with the dispose and the finalize functions.
I’ve read GObject – 2.0: GObject Tutorial, I’d like to understand what to put in there and why.
Furthermore, I think that I understand the global use of these functions, they are the GObject equivalent of the C free() function, but, from browsing the code of applications using GObject like Gnome-Calandar, what I see in these functions is not what I expect.
I was thinking that all fields of any class that are pointer like, for example, GtkWidget *awidget, should be freed with g_clear_object in dispose and/or (?) finalize. Likewise, I’m not sure of where to use gtk_widget_unparent, am-I right if I say that it should be called in my custom widget class’s dispose function ?
Thanks for helping me out !!
(here is the repository of the app I’m creating to learn GTK and GObject GitHub - Xalares/gread: A little app to train for fast reading technics)
GObjectClass.dispose is the right place to release resources. However, this is the guiding principal I think of when choosing
Does my object create things in
init that it expects to be there for the lifetime of the object? If so, and the data won’t cause circular references, I pair it with
finalize. This is because it can be possible, particularly with async operations or
GSourceFunc that a method could get called after your
dispose has been called. So unless you’ve checked your internal state at the method boundaries, you might expect something like
self->items to be non-
NULL which it could be if it was cleared in
If I have things that can cause circular references, I try to put that in
dispose and call
g_object_run_dispose() from the “most important” object in the circular graph to cause circular references to break. Of course, we generally try to avoid circular reference, but that isn’t always possible.
If they are just data objects and having
NULL values on fields are fine (the vast majority of objects), just clear state in
One case where I do both, however, is when I want to maintain symmetry between
finalize to maintain those internal invariants, but also want to ensure related objects are released. In that case, I might use something like
dispose but actually free the containing structure in
Hope that helps!
Thanks for your answer, it helps indeed. I’m more preoccupied now by the gtk behavior, I mean what are the needed method to put in dispose or finalize that are specific of gtk and needed when you subclass the widget class ?
The only things that are specific to GTK are:
gtk_widget_unparent() on direct children of your widget that you called
gtk_widget_dispose_template() to counterbalance
Both are meant to be called inside your
For versions of GTK older than 4.8, you should call
gtk_widget_unparent() on template children that are direct children of your widget, instead of
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.