Re: glib ref counting problem



Hi Tristan,

Tristan Van Berkom wrote:
Stefan Kost wrote:

hi hi,

hope that someone can enlighten me with my understanding of the gobject
ref-counting regarding to object destruction.

I have a gobject called BtSong. Upon construction it creates instances of
BtPattern, BtSetup and son on where it passes itself along.
Thus each child object now holds a reference to the BtSong object.
When the application that initialy created the BtSong instance is
done, it would
like to destroy the object with all the accosiated ressources. It
therefore
calles g_object_unref(song);.


If BtSong is the "owner" of BtPattern and BtSetup, then BtSong should
simply hold the initial reference on BtSetup and BtPattern (i.e. every
object is "born" with a `ref_count = 1'. Thus in bt_song_finalize(),
BtSong will release its reference on BtPattern and BtSetup, leading to
their destruction.
Unrefing must be done in dispose as far as I know.
If for example, BtPattern is needed after BtSong's
destruction, then the code which requires its existance, should hold
a reference to BtPattern and release it when it is no longer needed.
No, the child objects are not needed.

I will try to make it clearer.
application creates the BtSong
  BtSong->refct=1
the songs instance init creates the private BtPattern and BtSetup instances
and passes itself to their constructiors.
  BtSong->refct=3
  BtPattern->refct=1
  BtSetup->refct=1
the application is doe with the song (and everything that belongs to it), thus
calls g_object_unref(song);
  BtSong->refct=2
because that is unequal to 0, the dispose handler of BtSong is not called (and
neither finalize is) and therefore I can't release the refs to BtPattern and
BtSetup.

I hope it is clearer now.

Ciao
  Stefan
-- 
      \|/            Stefan Kost
     <@ @>           private            business
+-oOO-(_)-OOo------------------------------------------------------ - - -  -   -
|       __  Address  Simildenstr. 5     HTWK Leipzig, Fb IMN, Postfach 301166
|      ///           04277 Leipzig      04251 Leipzig
| __  ///            Germany            Germany
| \\\///    Phone    +49341 2253538     +49341 30766101
|  \__/     EMail    st_kost_at_gmx.net kost_at_imn.htwk-leipzig.de
|           WWW      www.sonicpulse.de  www.imn.htwk-leipzig.de/~kost/about.html
===-=-=--=---=---------------------------------- - - -  -    -


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