Modify PPM Config files to fix memory leak
authorYounghyunJoo <yh_.joo@samsung.com>
Tue, 21 Apr 2015 02:16:16 +0000 (11:16 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 21 Apr 2015 05:32:57 +0000 (05:32 +0000)
When parse the pluginmanager.xml file, memory leaks are occured
To fix memory leak, modify xml pasing function in the Config file

Change-Id: I1ee0b7a0ec6b3d09428fc18bd0006d97f81b21e3
Signed-off-by: YounghyunJoo <yh_.joo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/790
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/protocol-plugin/plugin-manager/src/Config.cpp
service/protocol-plugin/plugin-manager/src/Config.h

index 4b07ac7..3eee100 100644 (file)
@@ -59,18 +59,25 @@ Config::~Config(void)
 PMRESULT Config::loadConfigFile(const std::string configfilepath)
 {
     // Read the xml file
-    std::ifstream xmlFile(configfilepath.c_str());
+    std::basic_ifstream< char > xmlFile(configfilepath.c_str());
     if (!xmlFile.good())
     {
         return PM_S_FALSE;
     }
-    xml_document<> doc;
-    //into a vector
-    std::vector<char> buffer((istreambuf_iterator<char>(xmlFile)), istreambuf_iterator<char>());
-    buffer.push_back('\0');
+    xml_document< char > doc;
 
-    // Parse the buffer using the xml file parsing library into doc
-    parsing(buffer, &doc);
+    xmlFile.seekg(0, std::ios::end);
+    unsigned int size = (unsigned int)xmlFile.tellg();
+    xmlFile.seekg(0);
+
+    std::vector< char > xmlData(size + 1);
+    xmlData[size] = 0;
+
+    xmlFile.read(&xmlData.front(), (std::streamsize)size);
+    xmlFile.close();
+
+    // Parse the xmlData using the xml file parsing library into doc
+    parsing(&xmlData.front(), &doc);
 
     // Find our root node
     xml_node<> *root_node = doc.first_node("pluginManager");
@@ -81,12 +88,12 @@ PMRESULT Config::loadConfigFile(const std::string configfilepath)
     return PM_S_OK;
 }
 
-PMRESULT Config::parsing(std::vector<char> buffer, xml_document<> *doc)
+PMRESULT Config::parsing(char *xmlData, xml_document<> *doc)
 {
-    // Parse the buffer using the xml file parsing library into doc
+    // Parse the xmlData using the xml file parsing library into doc
     try
     {
-        doc->parse<0>(&buffer[0]);
+        doc->parse< 0 >(xmlData);
     }
     catch (rapidxml::parse_error err)
     {
index ae73065..ae68956 100644 (file)
@@ -115,7 +115,7 @@ namespace OIC
             }
             void setValue(const std::string key, const std::string value);
             PMRESULT loadConfigFile(const std::string configfilepath);
-            PMRESULT parsing(std::vector<char> buffer, xml_document<> *doc);
+            PMRESULT parsing(char *xmlData, xml_document<> *doc);
             PMRESULT getXmlData(  xml_node<> *pluginInfo, std::string key);
     };
 }