Re: reference counting

Chaoron <[email protected]>
Newsgroups gmane.comp.gnome.gdome
Message-ID <[email protected]>
Here is the code for the above example:

#include <gdome.h>

#define ENABLE_SEGFAULT 0

static void traverse_tree(GdomeNode *root);

static gchar *treetext = "<?xml version=\"1.0\"?>\n" \
                          "<root><child/></root>\n";

int
main(int argc, char *argv[])
{
   GdomeDOMImplementation *imp;
   GdomeDocument *doc;
   GdomeNode *root, *node;
   GdomeDOMString *domstr;
   GdomeException exc;


/* Parse tree */
     imp = gdome_di_mkref();
   doc = gdome_di_createDocFromMemory(imp, treetext, 
GDOME_LOAD_PARSING, &exc);
   gdome_di_unref(imp, &exc);

   root = (GdomeNode *) gdome_doc_documentElement(doc, &exc);

   traverse_tree(root);
     gdome_n_unref(root, &exc);
   gdome_doc_unref(doc, &exc);

   /* Create tree "by hand" */

   domstr = gdome_str_mkref("root");
   imp = gdome_di_mkref();
   doc = gdome_di_createDocument(imp, NULL, domstr, NULL, &exc);
   gdome_di_unref(imp, &exc);
   gdome_str_unref(domstr);
     root = (GdomeNode *) gdome_doc_documentElement(doc, &exc);
     domstr = gdome_str_mkref("child");
   node = (GdomeNode *) gdome_doc_createElement(doc, domstr, &exc);
   gdome_str_unref(domstr);
     gdome_n_appendChild(root, node, &exc);
   gdome_n_unref(node, &exc);
     traverse_tree(root);
     gdome_n_unref(node, &exc);
   gdome_n_unref(root, &exc);
   gdome_doc_unref(doc, &exc);

     return 0;
}

static void
traverse_tree(GdomeNode *root)
{
   GdomeNode *node;
   GdomeException exc;
       /* (Do something with the current root) */
     node = gdome_n_firstChild(root, &exc);
   while (node != NULL)
     {
       traverse_tree(node);
       #if ENABLE_SEGFAULT == 1
       gdome_n_unref(node, &exc); /* !!! Leak or segfault !!! */
#endif
       node = gdome_n_nextSibling(node, &exc);
     }
}


I hope it helps you understand the problem.

   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.