From 7dac88cd325e88dbff057ba9b0c4e27d02e67ff2 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 Jul 2010 20:18:02 +0200 Subject: [PATCH] feature #282: Thread saefty & memory leaks for XML parsing --- src/nebula/Nebula.cc | 9 +++++++++ src/template/Template.cc | 2 ++ src/template/test/template.cc | 30 +++++++++++++++++------------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index d6ab6b64d3..6d7637e11b 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -115,6 +116,11 @@ void Nebula::start() NebulaLog::log("ONE",Log::INFO,os); + // ----------------------------------------------------------- + // Initialize the XML library + // ----------------------------------------------------------- + xmlInitParser(); + // ----------------------------------------------------------- // Pools // ----------------------------------------------------------- @@ -532,6 +538,9 @@ void Nebula::start() pthread_join(rm->get_thread_id(),0); pthread_join(hm->get_thread_id(),0); + //XML Library + xmlCleanupParser(); + NebulaLog::log("ONE", Log::INFO, "All modules finalized, exiting.\n"); } diff --git a/src/template/Template.cc b/src/template/Template.cc index 6cd0be3723..31e300f071 100644 --- a/src/template/Template.cc +++ b/src/template/Template.cc @@ -485,6 +485,8 @@ int Template::from_xml(const string &xml_str) } } + xmlFreeDoc(xml_doc); + return 0; } diff --git a/src/template/test/template.cc b/src/template/test/template.cc index 7f2faeb466..dfdd060a18 100644 --- a/src/template/test/template.cc +++ b/src/template/test/template.cc @@ -15,7 +15,7 @@ using namespace std; /* ************************************************************************* */ /* ************************************************************************* */ -class TemplateTest : public CppUnit::TestFixture +class TemplateTest : public CppUnit::TestFixture { private: Template *t, *tr, *t1; @@ -32,7 +32,7 @@ public: TemplateTest() { - test_ok = + test_ok = "#This line is a comment\n" " # Other comment\n" "MEMORY=345 # more comments behind an attribute\n" @@ -89,6 +89,8 @@ public: t1= new Template(); t1->parse(test_ok,&error); + + xmlInitParser(); } void tearDown() @@ -96,6 +98,8 @@ public: delete t; delete tr; delete t1; + + xmlCleanupParser(); } /* ********************************************************************* */ @@ -108,7 +112,7 @@ public: string tmp; rc = t->parse(test_ok,&error); - + if ( error != 0 ) { cerr << error << endl; @@ -139,7 +143,7 @@ public: CPPUNIT_ASSERT(test_ok_xml == tmp); } - + /* --------------------------------------------------------------------- */ void test_str() @@ -157,21 +161,21 @@ public: { vector attrs; string *tmp; - + CPPUNIT_ASSERT(t1->get("DISK",attrs) == 2 ); CPPUNIT_ASSERT(attrs[0]->type() == Attribute::VECTOR); tmp = attrs[0]->to_xml(); - CPPUNIT_ASSERT( *tmp == + CPPUNIT_ASSERT( *tmp == "" ""); delete tmp; CPPUNIT_ASSERT(attrs[1]->type() == Attribute::VECTOR); - + tmp = attrs[1]->to_xml(); - CPPUNIT_ASSERT( *tmp == + CPPUNIT_ASSERT( *tmp == "" ""); delete tmp; @@ -179,7 +183,7 @@ public: CPPUNIT_ASSERT(t1->get("CPU",attrs) == 1 ); CPPUNIT_ASSERT(attrs[2]->type() == Attribute::SIMPLE); - + tmp = attrs[2]->to_xml(); CPPUNIT_ASSERT( *tmp == ""); delete tmp; @@ -203,7 +207,7 @@ public: void test_remove() { vector attrs; - + string t1_xml; string rm_xml=""; string xml; - + string nattr = "XTRA"; string vattr = "44"; @@ -351,7 +355,7 @@ public: ts->addTest(new CppUnit::TestCaller( "parse() Test", &TemplateTest::test_parser)); - + ts->addTest(new CppUnit::TestCaller( "marshall() Test", &TemplateTest::test_marshall)); @@ -395,7 +399,7 @@ public: int main(int argc, char ** argv) { CppUnit::TextUi::TestRunner tr; - + tr.addTest(TemplateTest::suite()); tr.run();