From: Igor L. S. <ism...@st...> - 2004-10-18 17:30:32
|
Hello Christophe, Monday, October 18, 2004, 5:29:04 PM, you wrote: CdV> Could you post the smallest program possible that reproduce the problem CdV> (possibly the one mentionned above) ? This would save much time for CdV> understanding what's going wrong. Here's the libxml2 test program that WORKS: ===test1.cpp============= BEGIN ======================================================== #include <libxml/parser.h> #include <libxml/tree.h> #include <string> #include <iostream> using namespace std; void parse_file_test(const std::string& filename); void parse_file_test(const std::string& filename) { xmlParserCtxtPtr ctxt; xmlDocPtr doc; ctxt = xmlNewParserCtxt(); if (ctxt == NULL) { std::cerr << "Failed to allocate parser context" << std::endl; return; } doc = xmlCtxtReadFile(ctxt, filename.c_str(), NULL, XML_PARSE_DTDVALID | XML_PARSE_NOENT ); if (doc == NULL) { std::cerr << "Failed to parse " << filename << std::endl; } else { if (ctxt->valid == 0) { std::cerr << "Failed to validate " << filename << std::endl; } else { FILE* f = NULL; f = fopen("res.xml", "w"); if (f) { xmlDocDump(f, doc); fclose(f); } } xmlFreeDoc(doc); } xmlFreeParserCtxt(ctxt); } int main(int _argc, char** _argv) { if (_argc < 2) { cerr << "File name missing" << endl; return 1; } string filename(_argv[1]); parse_file_test(filename); return 0; } ===test1.cpp============= END ======================================================== I then take the function parse_file_test() and copy-paste it to parsers/domparser.cc and declare it in parsers/domparser.h outside the xmlpp namespace like this: ... #include <libxml++/api_export.h> extern void parse_file_test(const std::string& filename); namespace xmlpp { ... And here's the test program I use to test parse_file_test() in libxml++: ===test2.cpp============= BEGIN ======================================================== #include <libxml++/libxml++.h> #include <iostream> int main(int argc, char* argv[]) { std::string filepath; if(argc > 1 ) filepath = argv[1]; //Allow the user to specify a different XML file to parse. else filepath = "example.xml"; try { parse_file_test(filepath); } catch(const std::exception& ex) { std::cout << "Exception caught: " << ex.what() << std::endl; } return 0; } ===test2.cpp============= END ======================================================== And here's the XML files I use as input: =====example.xml==============BEGIN========================================= <?xml version="1.0"?> <!DOCTYPE example PUBLIC "" "example.dtd" [ <!ENTITY wwwmurrayc SYSTEM "entity.xml"> <!ENTITY wwwlibxmlplusplus "http://libxmlplusplus.sourceforge.net"> ]> <example> <examplechild id="1"> <child_of_child> &wwwmurrayc; </child_of_child> </examplechild> </example> ======example.xml=============END=========================================== ======entity.xml============BEGIN=========================================== Hello! ======entity.xml============END============================================= test1.cpp runs just fine - it produces an xml file res.xml as follows: <?xml version="1.0"?> <!DOCTYPE example PUBLIC "" "example.dtd" [ <!ENTITY wwwmurrayc SYSTEM "entity.xml"> <!ENTITY wwwlibxmlplusplus "http://libxmlplusplus.sourceforge.net"> ]> <example> <examplechild id="1"> <child_of_child> Hello! </child_of_child> </examplechild> </example> Alas, test2.cpp crashes. When I remove xmlFreeDoc(doc) from parse_file_test() in libxml++, it also works just fine. Otherwise it crashes: (gdb) bt #0 0x00000011 in ?? () #1 0x0806d184 in xmlFreeNodeList (cur=0x812b070) at tree.c:3282 #2 0x0806d057 in xmlFreeNodeList (cur=0x812a4a8) at tree.c:3286 #3 0x0806d057 in xmlFreeNodeList (cur=0x8129f38) at tree.c:3286 #4 0x0806b05d in xmlFreeDoc (cur=0x8117348) at tree.c:1126 #5 0x0804c9ae in parse_file_test(std::string const&) (filename=@0xbffff7b0) at domparser.cc:277 #6 0x0804b52f in main () #7 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6 N.B. domparser.cc:277 - this is where xmlFreeDoc(doc) of parse_file_test() is in my case. So what's happening? Am I doing something wrong? -- Best regards, Igor mailto:ism...@st... |