Re: [xml] Help: memory leak ??



Thank you  very much for your comment and advice.
It was very helpful to me.
Your point is right. there are no memory leaks anymore.

Actually I'm afraid to say that for xml newbieslike me there are not
much references or manuals for libxml, especially for API.
Furthermore It was very hard to find some good example codes with explanation.
If exists, plz let me know that.

Currently the only one of my reference is just http://xmlsoft.org
Is there any method to understand an internal mechanism of  each API's
malloc or free except reading the source itself ? I can't find some
good text for satisfying this purpose.

Best regards,



2007/2/16, William M. Brack <wbrack mmm com hk>:
The first thing which you must do when you encounter a problem with your
code is to *very carefully* debug your logic.  It is really not fair to
expect the libxml2 developers and other list members to do this for you.
When this problem was first sent in, the obvious error in not freeing the
return value from xmlGetProp was kindly pointed out by Daniel.  There was
certainly no promise, however, that this was the *only* error.

The remaining "memory leak" is caused by the totally incorrect handling
of cur_node->properties, which is basically corrupting the internal tree.
 One way of correcting this particular error would be to replace the
"while" loop code with:

  xmlAttrPtr propPtr;
    ....
      propPtr = cur_node->properties;
      while( propPtr ) {
          key = (xmlChar*) propPtr->name;
          value = xmlGetProp( cur_node, key);
          printf("%s/%s = %s\n", category, key, value);
          xmlFree(value);
          propPtr = propPtr->next;
      }

Note, however, that there is still *no guarantee* that there are not
other errors within your code.

Regards,

Bill

Taeyoung Hong wrote:
> I tested the following YuChan's test code by adding "xmlFree(value)"
> in while loop like this;
>
>                while( cur_node->properties ) {
>                         key = (xmlChar*) cur_node->properties->name;
>                         value = xmlGetProp( cur_node, key);
>                         printf("%s/%s = %s\n", category, key, value);
>                         xmlFree(value);
>                         cur_node->properties =
> cur_node->properties->next;
>                 }
>
> However, still memory leak occurs.
> On one hundred iteration of test() fuction in the code, resident
> memory size of the program increased up to 88 KB.
> testbed is the linux w/ 2.6.9-5.0.5.ELsmp, x86_64, opteron, CentOS 4.0,
> and rpm libxml2-2.6.16-6 was installed
>
> the same test without xmlFree(value) gives the 96 KB memory leaks.
>
> when I tried to test the simplified code like this, which gave us the
> same result,
> 88KB memory leaks,
>
>                while( cur_node->properties ) {
>                                             cur_node->properties =
> cur_node->properties->next;
>                 }
>
> I do not know where this leak comes from.
> Plz, let me know that
>
>
>
>
>
>
> ------------------------------------------------
> Re: [xml] Help: memory leak ??
> Daniel Veillard
> Tue, 22 Aug 2006 02:58:06 -0700
> On Tue, Aug 22, 2006 at 05:07:27PM +0900, YuChan Park wrote:
>> during running the program it comsume 8 or 4 bytes memory whenever call
>> test
>> function included in blow code.
> ...
>>                        value = xmlGetProp( cur_node, key);
>
>   http://xmlsoft.org/html/libxml-tree.html#xmlGetProp
>
>   Returns:      the attribute value or NULL if not found. It's up to the
> caller
> to free the memory with xmlFree().
>
>   no amount of asking will ever replace reading the docs.
>
> Daniel
>
> --
> Red Hat Virtualization group http://redhat.com/virtualization/
> Daniel Veillard      | virtualization library  http://libvirt.org/
> [EMAIL PROTECTED]  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
> http://veillard.com/ | Rpmfind RPM search engine  http://rpmfind.net/
>
>
> 
----------------------------------------------------------------------------------------------------------------
>
> [xml] Help: memory leak ??
>
> YuChan Park
> Tue, 22 Aug 2006 01:07:39 -0700
> Hello.
>
> I wrote some application with libxml2 ( version 2.6.23 ).
>
> but the application comsume the system memory
>
> So for testing i tried to run example program which was written as
> like my program.
> but the status is same .
>
> during running the program it comsume 8 or 4 bytes memory whenever
> call test function included in blow code.
>
> the memory status is observated using top( linux program)
>
> RES included in top is increased continuously during running program..
>
> why does this problem appear??
>
> thanks.
>
> ------------------CODE---------------------------------------------
> #include <stdio.h>
> #include <libxml/parser.h>
> #include <libxml/tree.h>
> #include <string.h>
>
>
> char *testxml = "<GETINFO><NODENAME name=\"main.diylinux.org\"
> command=\"GETINFO\" /> \
>                   <cpu load=\"3010.714\" /> \
>                   <UPTIME>53768.39 52310.89</UPTIME></GETINFO>";
>
> static void
> print_element_names(xmlNode * a_node)
> {
>     xmlNode *cur_node = NULL;
>     xmlChar *key;
>     xmlChar *category;
>     xmlChar  *keys;
>     xmlChar *value;
>
>     for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
>
>         if (cur_node->type == XML_ELEMENT_NODE) {
>
>                 category = (xmlChar*)cur_node->name;
>                 while( cur_node->properties ) {
>                         key = (xmlChar*) cur_node->properties->name;
>                         value = xmlGetProp( cur_node, key);
>                         //printf("%s/%s = %s\n", category, key, value);
>
>                         cur_node->properties =
> cur_node->properties->next;
>                 }
>         }
>
>         print_element_names(cur_node->children);
>     }
>
> }
>
>
> int
> test()
> {
>     xmlDoc *doc = NULL;
>     xmlNode *root_element = NULL;
>
>     //if (argc != 2)
>      //   return(1);
>
>     LIBXML_TEST_VERSION
>
>     /*parse the file and get the DOM */
>     //doc = xmlReadFile(argv[1], NULL, 0);
>     doc = xmlParseMemory( testxml, strlen( testxml ) );
>
>     if (doc == NULL) {
>         return 1;
>     }
>
>     /*Get the root element node */
>     root_element = xmlDocGetRootElement(doc);
>     print_element_names(root_element);
>
>     xmlFreeDoc(doc);
>
>      xmlCleanupParser();
>
>     return 0;
> }
>
>
> int main(void)
> {
>         int i;
>         for( i =0; i<100; i++) {
>                 test();
>                 sleep(1);
>         }
>         return 0;
> }
>
> -------------------------------------------------END CODE--------------
> compile:
> gcc -o ttest test.c `libxml2-config --cflags --libs`
>
> ./ttest
>
> status: top -p `pidof ttest`
> the RES in top  increased continuously
> _______________________________________________
> xml mailing list, project page  http://xmlsoft.org/
> xml gnome org
> http://mail.gnome.org/mailman/listinfo/xml
>






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