Re: reference counting again

Chaoron <[email protected]>
Newsgroups gmane.comp.gnome.gdome
Message-ID <[email protected]>
The endless thread continues... :-)

> newnode = gdome_n_cloneNode(node, TRUE, &exc);
> root = (GdomeNode*) gdome_doc_documentElement(self, &exc);
> 
> while ((tmpnode = gdome_n_firstChild(newnode, &exc)) != NULL) {
>   GdomeNode* n1 = gdome_n_removeChild(newnode, tmpnode, &exc);
>   GdomeNode* n2 = gdome_n_appendChild(root, n1, &exc);
>   gdome_n_unref(tmpnode, &exc);
>   gdome_n_unref(n1, &exc);
>   gdome_n_unref(n2, &exc);
> }

In my case node (and so newnode) created from a different document, so 
n1 cannot be appended to the other document's root. I haven't tried it 
yet though.


> you are right, but that's because appendChild _does_return_ the 
> appended node. So it's better (even though more verbose) to create 
> another temp variable and to unref that variable instead, as I've 
> done in the example above. It's much more clear and doesn't rely on 
> Gdome2 side effects.
> Unfortunately, when using C + reference counting, you cannot just 
> forget about useless returned values ;-(

That's the cause I think returning with a new reference in appendChild 
like functions are unnecessary and make it hard to read and/or debug 
the C code. For example GTK has reference counting, but object query 
functions do not make another reference, instead you should use 
g_object_ref() on returned object if you really want to hold that 
reference. And it's very easy to automaticaly increase reference
count in language bindings with gdome_n_ref(). This could be done by 
modifying gdome_xml_n_mkref() to only create Gdome_xml_Node if it not 
already exists. But it's just an idea, too many programs would be
concerned.

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.