Search code examples
clibxml2

xmlReaderForMemory crashes on 2nd time call


after making google for long time also, i am unable to find reason/solution for crashing of xmlReaderForMemory,still with valid parameters.

i have created two parser function using libxml,when i call individually they are working fine.But when i call one after another it is getting crashed on xmlReaderForMemory by giving error s follows:

First-chance exception at 0x7c918fea in nayak.exe: 0xC0000005: Access violation writing location 0x00000010. Unhandled exception at 0x7c918fea in nayak.exe: 0xC0000005: Access violation writing location 0x00000010.

now i am giving the code of the two functions:

FIRST FUNCTION:

 char* CB_omniParser(char *omnistring){

        char *parseResult,;
        const char *fileName = omnistring;   
        char *temp,*texttemp,*result=0; 
        int i,len=0,error;
        xmlTextReaderPtr reader;
        len= strlen(omnistring);
        if(len==0)
                    return 0;   
                reader = xmlReaderForMemory(fileName,len,"",NULL,0);    

        if(reader){

            temp = (char *) GlobalAlloc(GPTR, sizeof(char)*len);
            parseResult = (char *) GlobalAlloc(GPTR,sizeof(char)*len+1);
            while(error=xmlTextReaderRead(reader)) {
                if(error==-1){              
                    return 0; // on failure
                }           
                switch(xmlTextReaderNodeType(reader)) {

                    case XML_READER_TYPE_ELEMENT: 

                        temp = (char *)xmlTextReaderConstName(reader);
                            strcat(parseResult,temp);                   
                            strcat(parseResult,"#");                            

                        xmlTextReaderMoveToElement(reader);                     
                          continue;

                    case XML_READER_TYPE_TEXT:  
                        temp = (char *)xmlTextReaderConstValue(reader); 
                                strcat(parseResult,temp);                           
                                strcat(parseResult,"|");                

                        continue;               

                }   

            }

            xmlFreeTextReader(reader);
            xmlCleanupParser();
            return parseResult;//on success returns the parsed omni string
        }
        else
            return 0; // on failure
    }

Second Function:

   char* CB_xmlParserFromMemory(char *xmlstring){
        char *xmlParseresult;   
        char *temp; 
        int i,len,,error;;

        xmlTextReaderPtr reader1;

        len= strlen(xmlstring);
        if(len==0)
            return 0;
        reader1 = xmlReaderForMemory(xmlstring,len,NULL,NULL,0);


        if(reader1){

            temp = (char *) GlobalAlloc(GPTR, sizeof(char)*len);
            while(error=xmlTextReaderRead(reader1)) {

                if(error==-1){
                    return 0; // on failure
                }           
                switch(xmlTextReaderNodeType(reader1)) {

                    case XML_READER_TYPE_ELEMENT: 

                        temp = (char *)xmlTextReaderConstName(reader1);                 

                            strcat(xmlParseresult,"\"");
                            strcat(xmlParseresult,temp);
                            strcat(xmlParseresult,"\"");
                            strcat(xmlParseresult,":");

                        xmlTextReaderMoveToElement(reader1);     
                          continue;

                    case XML_READER_TYPE_TEXT:              

                        temp = (char *)xmlTextReaderConstValue(reader1); 
                        strcat(xmlParseresult,"\"");
                        strcat(xmlParseresult,temp);
                        strcat(xmlParseresult,"\"");
                        strcat(xmlParseresult,",");
                    continue;               

                }   

            }       
            xmlCleanupParser();     
            xmlFreeTextReader(reader1);     
            GlobalFree(temp);
            return xmlParseresult;//on success returns the parsed omni string   
        }
        else
            return 0; // on failure
    }

both the functions are working individually fine.but if i call one function after another then both crashes at above given place...ith same error..plz help me.....


Solution

  • I think it's a lucky day for me as i am having the opportunity to answer my own question...

    Now i am happy as it is working perfectly fine at my end with out any issue, the issue was of memory(it's not that, what you are thinking after listening the word issue of memory).

    The issue was being raised because of the statement:

         xmlCleanupParser();
    

    as i have used instead of

       xmlInitParser ();
    

    but now i will not give the reason,because you guys should also do some work...

    I will give you the the URL which helped me to get out of this....