Re: Recommended way to cleanup your own gobject types in a desktop app (finalize and/or dispose)



Hello,

iirc, the GObject documentation states that dispose may be called
multiple times,
and the object must still be usable even after dispose is called, so
freeing memory
in dispose is wrong, even with locks.

Actually both dispose and finalize are needed if your object hold
references to other
objects, and has allocated memory during its init. The dispose will be called as
many times as necessary to remove the references to other object (and thus break
possible cycles), and when the object's ref count drop to 0, the
finalize is called to
completely free any resource held by the object.

--
Ali

2008/3/22 Philip Withnall <philip tecnocode co uk>:
> Hi,
>
>  For Totem we use dispose to unref any objects and finalize to free any
>  private memory and close any sockets/DB connections/other fluff, which
>  is (I believe), how you're supposed to do it.
>
>  If you only use dispose you'd have to put a lock variable in to stop any
>  private memory getting freed multiple times if dispose is called more
>  than once.
>
>  If you only use finalize then you could theoretically get unbreakable
>  cyclic references.
>
>  At the end of the day, if it works then it's probably OK; you just might
>  run into maintenance issues further down the line if you forget that you
>  didn't protect the call to g_free on your private memory, or that you
>  didn't cater for cyclic references by using dispose.
>
>  Regards,
>  Philip
>
>
>
>  On Sat, 2008-03-22 at 23:23 +0100, Jaap A. Haitsma wrote:
>  > Hi,
>  >
>  > When I look at projects like gnome-screensaver and gnome-power-manager
>  > I see that for their own created gobject types only the finalize
>  > function is implemented to cleanup the resources in the end of the
>  > application.
>  >
>  > gnome-volume-control uses only implements the dispose function
>  >
>  > I understand from [1] that the two cleanup function dispose and
>  > finalize exist to make bindings to languages with garbage collection
>  > easier. So I guess for libraries for which other language bindings
>  > might be written it's a good thing to follow the recommendation in [1]
>  > of what to do in finalize and dispose.
>  >
>  > So what is recommended for applications?
>  > 1) implement dispose and finalize as recommended
>  > 2) only finalize
>  > 3) only dispose
>  >
>  > Jaap
>  >
>  > [1] http://library.gnome.org/devel/gobject/unstable/gobject-memory.html
>  > _______________________________________________
>  > desktop-devel-list mailing list
>  > desktop-devel-list gnome org
>  > http://mail.gnome.org/mailman/listinfo/desktop-devel-list
>
> _______________________________________________
>  desktop-devel-list mailing list
>  desktop-devel-list gnome org
>  http://mail.gnome.org/mailman/listinfo/desktop-devel-list
>


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