Re: reference counting again

Chaoron <[email protected]>
Newsgroups gmane.comp.gnome.gdome
Message-ID <[email protected]>
Again. Let's see the critical iteration!

   while ((tmpnode = gdome_n_firstChild(newnode, &exc)) != NULL)
     {
       g_print("lives: %d\n", gdome_treegc_livenodes(tmpnode));
       gdome_n_appendChild(root, tmpnode, &exc);
       gdome_n_unref(tmpnode, &exc);
//      gdome_n_unref(tmpnode, &exc);
       g_print("lives left: %d\n\n", gdome_treegc_livenodes(tmpnode));
     }

   gdome_n_firstChild() creates the GdomeNode object and also increases 
the number of live nodes. As you can see in the output, it's number of 
live nodes are 2. Gdome_n_appendChild() also increases the reference 
count of tmpnode (and so the no. of live nodes). So that I should 
dereference tmpnode two times, if I don't want my program leak memory. 
Just try to run the program in memprof, it shows one GdomeNode object 
leaks per child. Also you can see the number of livenodes are 2 on each 
child at the end of the iteration.
   However, dereferencing tmpnode the second time looks also decrease 
the reference of it's sibling. It looks correct only for the first 
child node: at the end of the iteration the number of live nodes equals 
one. But the next node returned by gdome_n_firstChild() only has one 
live node, even though nobody wanted to dereference that node.
   If you don't use gdome_treegc_livenodes(), the iteration may not 
fail, but the heap is become corrupted. For me it returned NULL getting 
the document element of new the document.

   I also check the real reference count of the (really a) 
Gdome_xml_Node object and this behaviour looks like an anomaly for me.

   A normal traversal of child nodes is simply the following iteration:
     node = gdome_n_firstChild(root, &exc);
   while (node != NULL)
     {
       /* do sg */
             save = node;
       node = gdome_n_nextSibling(save, &exc);
       gdome_n_unref(save, &exc);
     }
     It's just the same, for a new document: you create a GdomeNode 
object by gdome_n_firstChild() and destroy that reference with 
gdome_n_unref() (without destroying the xmlNode inside GdomeNode).

   Any working solution for this problem? I really need to have a
function creating a new document from any subtree. Some parts are
missing from the example code such as namespace handling and importing
attributes, but I already have code for these.

   Daniel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.