Xerces segmentation fault

Discussion in 'XML' started by mearvk, Feb 9, 2008.

  1. mearvk

    mearvk Guest

    Running this on Redhat I get a segmentation fault in parseResultData's
    for-loop during its second iteration. Anything obviously wrong with my
    code?

    Thanks.


    [CODE main.C ]

    #include <string>
    #include <iostream>
    #include "DDAS_XML_Handler.hpp"

    extern "C"
    {
    #include "lip.h"
    }

    using namespace std;

    int main()
    {
    DDAS_XML_Handler* handler=new DDAS_XML_Handler();

    DDAS_VALIDATION_STRUCT result = handler-
    >parseResultData( "result.xml" );


    for(unsigned int i=0; i<result.size; i++)
    {
    cout<<"Result #";
    zwrite(result.def_nums);
    cout<<" has SSE: "<<result.SSEs<<endl;
    }
    }

    [/CODE]

    [CODE DDAS_XML_Handler.C (partial)]

    //parse xml document containing boinc result data
    DDAS_VALIDATION_STRUCT DDAS_XML_Handler::parseResultData(const
    char* file_path)
    {
    DDAS_VALIDATION_STRUCT retval;

    DOMAttr* sse_attr;
    DOMAttr* def_num_attr;
    DOMDocument* doc;
    DOMNodeList* data_rows;
    DOMElement* current;

    XercesDOMParser* parser;

    try
    {
    XMLCh* data_row_xmlch =
    XMLString::transcode("data_row");
    XMLCh* sse_xmlch =
    XMLString::transcode("SSE");
    XMLCh* def_num_xmlch =
    XMLString::transcode("definition_number");

    parser = new XercesDOMParser();
    parser-
    >setValidationScheme(XercesDOMParser::Val_Always);

    parser->setDoNamespaces(false);
    parser->setDoSchema(false);
    parser->setLoadExternalDTD(false);
    parser->parse(file_path);

    doc = parser->getDocument();
    data_rows = doc-
    >getElementsByTagName(data_row_xmlch);


    retval.size = data_rows->getLength();
    retval.SSEs = new double[ retval.size ];
    retval.def_nums = new verylong[ retval.size ];

    for(unsigned int i=0; i<retval.size; i++)
    {
    //current data_row
    current =
    dynamic_cast<DOMElement*> (data_rows->item(i));

    //get current's attribute nodes
    sse_attr = current-
    >getAttributeNode( sse_xmlch );

    def_num_attr = current-
    >getAttributeNode( def_num_xmlch );


    //convert attribute values from XMLCh*
    to char*
    char* sse_ch = new
    char[128];
    char* def_num_ch = new
    char[128];
    sse_ch =
    XMLString::transcode( sse_attr->getValue() );
    def_num_ch =
    XMLString::transcode( def_num_attr->getValue() );

    //convert from char* to numeric
    representation
    retval.SSEs =
    atof( sse_ch );
    zsread( def_num_ch ,
    &retval.def_nums );

    //free dynamic memory
    free(sse_ch);
    free(def_num_ch);
    }
    }
    catch( xercesc::XMLException& e)
    {
    char* message =
    XMLString::transcode( e.getMessage() );
    cout << "Error parsing XML document: " <<
    message << endl;
    XMLString::release( &message );
    }

    return retval;

    [/CODE]

    [CODE DDAS_XML_Handler.hpp]

    extern "C"
    {
    #include "lip.h"
    }

    struct DDAS_VALIDATION_STRUCT
    {
    double* SSEs;
    unsigned int size;
    verylong* def_nums;
    };


    class DDAS_XML_Handler
    {
    private:
    // char* URL;

    public:
    //constructor
    DDAS_XML_Handler();

    //destructor
    ~DDAS_XML_Handler();

    //returns a node list given a URL and a node name
    //xercesc::DOMNodeList* getNodeList(char* node_name);

    //converts client's result data from XML file to form
    appropriate for validation
    DDAS_VALIDATION_STRUCT
    DDAS_XML_Handler::parseResultData(const char* file_path);


    //prints all matching attributes from a DOMNodeList
    //void printNodeListAttrs(xercesc::DOMNodeList*
    node_list, char* node_attr);

    //prints matching attribute for a given DOMNode
    //void printNodeAttr(xercesc::DOMNode* node, char*
    node_attr);
    };

    [/CODE]


    [DATA result.xml ]


    <DDAS_result_set>

    <data_row SSE="138.328873" binary="11101101"
    definition_number="237">
    <regression_coefficient id="7" value="0.023954"/>
    <regression_coefficient id="6" value="0.003977"/>
    <regression_coefficient id="5" value="-0.004866"/>
    <regression_coefficient id="3" value="0.000086"/>
    <regression_coefficient id="2" value="0.004871"/>
    <regression_coefficient id="0" value="-0.000803"/>
    </data_row>

    <data_row SSE="137.649657" binary="11110110"
    definition_number="246">
    <regression_coefficient id="7" value="0.022691"/>
    <regression_coefficient id="6" value="0.003805"/>
    <regression_coefficient id="5" value="-0.005100"/>
    <regression_coefficient id="4" value="0.000317"/>
    <regression_coefficient id="2" value="0.003974"/>
    <regression_coefficient id="1" value="0.096372"/>
    </data_row>

    <data_row SSE="137.549310" binary="11101111"
    definition_number="239">
    <regression_coefficient id="7" value="0.024656"/>
    <regression_coefficient id="6" value="0.003862"/>
    <regression_coefficient id="5" value="-0.004905"/>
    <regression_coefficient id="3" value="0.000051"/>
    <regression_coefficient id="2" value="0.004222"/>
    <regression_coefficient id="1" value="0.098256"/>
    <regression_coefficient id="0" value="-0.001051"/>
    </data_row>

    <data_row SSE="137.628296" binary="11101110"
    definition_number="238">
    <regression_coefficient id="7" value="0.022820"/>
    <regression_coefficient id="6" value="0.003741"/>
    <regression_coefficient id="5" value="-0.005057"/>
    <regression_coefficient id="3" value="0.000065"/>
    <regression_coefficient id="2" value="0.004152"/>
    <regression_coefficient id="1" value="0.095907"/>
    </data_row>

    <data_row SSE="137.665670" binary="11100110"
    definition_number="230">
    <regression_coefficient id="7" value="0.022538"/>
    <regression_coefficient id="6" value="0.003791"/>
    <regression_coefficient id="5" value="-0.005077"/>
    <regression_coefficient id="2" value="0.004172"/>
    <regression_coefficient id="1" value="0.098258"/>
    </data_row>

    <data_row SSE="137.572043" binary="11100111"
    definition_number="231">
    <regression_coefficient id="7" value="0.024580"/>
    <regression_coefficient id="6" value="0.003910"/>
    <regression_coefficient id="5" value="-0.004909"/>
    <regression_coefficient id="2" value="0.004243"/>
    <regression_coefficient id="1" value="0.100252"/>
    <regression_coefficient id="0" value="-0.001132"/>
    </data_row>

    <data_row SSE="138.316077" binary="11111101"
    definition_number="253">
    <regression_coefficient id="7" value="0.023960"/>
    <regression_coefficient id="6" value="0.003998"/>
    <regression_coefficient id="5" value="-0.004901"/>
    <regression_coefficient id="4" value="0.000313"/>
    <regression_coefficient id="3" value="0.000069"/>
    <regression_coefficient id="2" value="0.004669"/>
    <regression_coefficient id="0" value="-0.000762"/>
    </data_row>

    <data_row SSE="138.342605" binary="11111011"
    definition_number="251">
    <regression_coefficient id="7" value="0.024320"/>
    <regression_coefficient id="6" value="0.004323"/>
    <regression_coefficient id="5" value="-0.004240"/>
    <regression_coefficient id="4" value="0.000961"/>
    <regression_coefficient id="3" value="0.000008"/>
    <regression_coefficient id="1" value="0.109715"/>
    <regression_coefficient id="0" value="-0.000738"/>
    </data_row>

    <data_row SSE="137.565598" binary="11110111"
    definition_number="247">
    <regression_coefficient id="7" value="0.024595"/>
    <regression_coefficient id="6" value="0.003914"/>
    <regression_coefficient id="5" value="-0.004930"/>
    <regression_coefficient id="4" value="0.000204"/>
    <regression_coefficient id="2" value="0.004113"/>
    <regression_coefficient id="1" value="0.098959"/>
    <regression_coefficient id="0" value="-0.001086"/>
    </data_row>

    <data_row SSE="137.625804" binary="11111110"
    definition_number="254">
    <regression_coefficient id="7" value="0.022853"/>
    <regression_coefficient id="6" value="0.003753"/>
    <regression_coefficient id="5" value="-0.005070"/>
    <regression_coefficient id="4" value="0.000138"/>
    <regression_coefficient id="3" value="0.000057"/>
    <regression_coefficient id="2" value="0.004068"/>
    <regression_coefficient id="1" value="0.095368"/>
    </data_row>

    </DDAS_result_set>

    [/DATA]
     
    mearvk, Feb 9, 2008
    #1
    1. Advertising

  2. mearvk

    P. Lepin Guest

    mearvk wrote:
    > Running this on Redhat I get a segmentation fault in parseResultData's
    > for-loop during its second iteration. Anything obviously wrong with my
    > code?


    Code:
    
    Obviously wrong. If you want help on Usenet, invest some effort into making
    your code readable to others (do not use tabs for indentation, make sure
    your code is formatted to fit 78 chars).
    [color=blue]
    >                                 char* sse_ch            = new
    > char[128];
    >                                 char* def_num_ch        = new
    > char[128];
    >                                 sse_ch                  =
    > XMLString::transcode( sse_attr->getValue() );
    >                                 def_num_ch              =
    > XMLString::transcode( def_num_attr->getValue() );[/color]
    
    Obviously wrong. XML spec does not guarantee an atrribute's value will fit
    in 128 octets.
    [color=blue]
    >                                 free(sse_ch);
    >                                 free(def_num_ch);[/color]
    
    Obviously wrong. See
    
      <http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.3>
    
    After this I didn't bother to look at it in greater detail. If you want a
    thorough grilling, you can always submit yourself to the Great Old Ones of
    comp.lang.c++ (note that they are only interested in issues relevant to
    ISO/ANSI Standard C++; questions relevant only to specific platforms and/or
    libraries will not be welcome).
    
    -- 
    Presented in Brain Control where available.
     
    P. Lepin, Feb 10, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    871
  2. Pud
    Replies:
    0
    Views:
    578
  3. cvissy
    Replies:
    0
    Views:
    609
    cvissy
    Nov 16, 2004
  4. Replies:
    0
    Views:
    532
  5. Ivan Vecerina
    Replies:
    0
    Views:
    488
    Ivan Vecerina
    Jun 29, 2003
Loading...

Share This Page