Re: [xml] Memory leak problem

  Hi Eric,

sorry, I'm very late to the thread, but in case you still have issues:

 1/ yes xmlFreeDoc is the right way to free the whole memory of the
    document, you may be missing freeing other bits left and right if you
    don't use it
 2/ you can debug memory allocation by running valgrind but there is another
    libxml2 only way:
    - recompile after configuring with --with-mem-debug , when doing so
      xmlFree() and free() are not interchangeable anymore but all allocations
      by libxml2 are tracked
    - xmlMemoryDump() then saves to a .memorylist file all the allocations
      made by libxml2 which were not freed by libxml2 you can also call it
      from a debugger to track dynamically
    that's how I removed most leaks in libxml2
 3/ valgrind is indeed the simplest tool if you can't recompile or
    if you mixed alloc and frees from the C library and libxml2 layers
    I would still encourage to avoid that mixing, some of the special features
    of the libxml2 DOM tree are that sometimes the same string should be freed
    sometime it can't e.g. textual node content.
 4/ the library use a dictionary which can be reused accross documents
    and only xmlCleanupParser() frees that up but you should be very careful
    about in threaded context or in programs potentially using libxml2 in
    other modules.

  that said I hope your problem is solved now, sorry for being so late


On Sat, Nov 23, 2019 at 10:53:01AM -0700, Eric Eberhard wrote:
I have used libxml2 since it was libxml so I can't believe I am having this
problem.  I'm looking for someone smarter than me to help out.


I use only the DOM (memory) parser, and an old version at that:


I have a program that makes many XML files.  They, alone, are not a problem.
But when a large amount are made (100s) it gets a memory error.


After each XML document I make I write them to disk and then free them
(meaning I thought I was freeing ALL the memory).  This is not the case so I
assume I am not coding it correctly.  This is what I am doing .


The node is passed to this function  (xmlNodePtr) named freeMe(xmlNsPtr


Void freeMe(xmlNSPtr node) {


xmlNsPtr next;                                             

 xmlNsPtr cur;                                              


 cur = node->ns;                                            

 while (cur) {                                              

     next = cur->next;                                      

      if (cur->href) xmlFree((char *) cur->href);    

      if (cur->prefix) xmlFree((char *) cur->prefix);

      cur->href = NULL;                              

      cur->prefix = NULL;                            

       xmlFree((char *)cur); 

      cur = next;                         


node->ns = NULL;                             


if (node->type == XML_ELEMENT_NODE ||                           

     node->type == XML_XINCLUDE_START ||                         

                              node->type == XML_XINCLUDE_END) {  

     cur = node->nsDef;                                          

     while (cur) {                                               

             next = cur->next;                                              

              if (cur->href) xmlFree((char *) cur->href); 

               if (cur->prefix) xmlFree((char *) cur->prefix

               cur->href = NULL;                           

               cur->prefix = NULL;                         

               xmlFree((char *)cur);

             cur = next;                                         


     node->nsDef = NULL;  

    If (node->children)

FreeMe(node->children);                            //recursive





//done I think freeing up all memory


I LEFT out the tests for NULLS and error checking and other silly things so
it would not be too long.


It also repeats mostly the exact same code if the node is NS so I won't bore
you.  Don't use NS much anyway.


Problem is, something is not free()'d up.  


So besides the main node and NS is there something else I have to free()?
Better, is there a function that I can pass the node and it free()'s
everything for me?


Any help - even sample code - would be much appreciated!  Free horse back
ride in AZ for a winner?










Eric S Eberhard

VICS (Vertical Integrated Computer Systems)

Voice: 928 567 3529

Cell    : 928 301 7537  (not reliable except for text or if not home)

2933 W Middle Verde Rd

Camp Verde, AZ  86322


Oh - help me out and please join this group.  I need to get large numbers of
members to take to the politicians.

This might work to just join?

<table border="0" width="100%" cellspacing="0" cellpadding="0"
style="border-collapse:collapse;"><tr style=""><td height="28"
style="line-height:28px;">&nbsp;</td></tr><tr><td style=""><table border="0"
width="280" cellspacing="0" cellpadding="0"
style="border-collapse:separate;background-color:#ffffff;border:1px solid
#dddfe2;border-radius:3px;font-family:Helvetica, Arial,
sans-serif;margin:0px auto;"><tr style="padding-bottom: 8px;"><td
style=""><img class="img"
" width="280" height="146" alt="" /></td></tr><tr><td
style="font-size:14px;font-weight:bold;padding:8px 8px 0px
8px;text-align:center;">Save The Middle Verde</td></tr><tr><td
ublic group . 228 members</td></tr><tr><td style="padding:8px 12px 12px
12px;"><table border="0" cellspacing="0" cellpadding="0"
48115&amp;source=email_campaign_plugin" target="_blank"
rel="noopener"><table border="0" cellspacing="0" cellpadding="3"
align="center" style="border-collapse:collapse;"><tr><td
style="border-bottom:3px solid #4267b2;border-top:3px solid
#4267b2;color:#FFF;font-family:Helvetica, Arial,
style="border-top:1px solid #dddfe2;font-size:12px;padding:8px 12px;">This
group is to help preserve the Middle Verde River, the Middle Verde Character
area as defined in the Town of Camp Verde&#039;s General Plan, the 260
co...</td></tr></table></td></tr><tr style=""><td height="28"


xml mailing list, project page
xml gnome org

Daniel Veillard      | Red Hat Developers Tools
veillard redhat com  | libxml Gnome XML XSLT toolkit | virtualization library

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