diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2014-06-02 15:45:28 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2014-06-02 15:45:28 +0200 |
commit | f87859b88f965a364804c22c1e81ca406df0e402 (patch) | |
tree | 43d40815ed22322a20e194bef683a4295eaf4d2d /test/source/xmltesttools.cxx | |
parent | 7e4a51ee504baedc9150a1c88c2a5ce7cd64bfbd (diff) |
Fix memory leaks in test code
Change-Id: Ifa756a5da636e0b37654ad83900115872bc40f0e
Diffstat (limited to 'test/source/xmltesttools.cxx')
-rw-r--r-- | test/source/xmltesttools.cxx | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/test/source/xmltesttools.cxx b/test/source/xmltesttools.cxx index 1cb231c0849d..0cb89f701201 100644 --- a/test/source/xmltesttools.cxx +++ b/test/source/xmltesttools.cxx @@ -32,12 +32,13 @@ xmlDocPtr XmlTestTools::parseXmlStream(SvStream* pStream) return xmlParseDoc(reinterpret_cast<xmlChar*>(pBuffer.get())); } -xmlNodeSetPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath) +xmlXPathObjectPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath) { xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc); registerNamespaces(pXmlXpathCtx); xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx); - return pXmlXpathObj->nodesetval; + xmlXPathFreeContext(pXmlXpathCtx); + return pXmlXpathObj; } void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/) @@ -46,24 +47,32 @@ void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/) OUString XmlTestTools::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute) { - xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); + xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath); + xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval; CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), 1, xmlXPathNodeSetGetLength(pXmlNodes)); if (rAttribute.isEmpty()) return OUString(); xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; - return OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr()))); + xmlChar * prop = xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr())); + OUString s(OUString::createFromAscii((const char*)prop)); + xmlFree(prop); + xmlXPathFreeObject(pXmlObj); + return s; } OUString XmlTestTools::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath) { - xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); + xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath); + xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval; CPPUNIT_ASSERT_MESSAGE(OString("XPath '" + rXPath + "' not found").getStr(), xmlXPathNodeSetGetLength(pXmlNodes) > 0); xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; - return OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)); + OUString s(OUString::createFromAscii((const char*)((pXmlNode->children[0]).content))); + xmlXPathFreeObject(pXmlObj); + return s; } void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue) @@ -75,9 +84,11 @@ void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const O void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes) { - xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); + xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath); + xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval; CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes)); + xmlXPathFreeObject(pXmlObj); } void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent) @@ -87,17 +98,20 @@ void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, void XmlTestTools::assertXPathChildren(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfChildNodes) { - xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); + xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath); + xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval; CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), 1, xmlXPathNodeSetGetLength(pXmlNodes)); xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of child-nodes is incorrect").getStr(), nNumberOfChildNodes, (int)xmlChildElementCount(pXmlNode)); + xmlXPathFreeObject(pXmlObj); } int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName) { - xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); + xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath); + xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval; CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), 1, xmlXPathNodeSetGetLength(pXmlNodes)); @@ -109,6 +123,7 @@ int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, con break; ++nRet; } + xmlXPathFreeObject(pXmlObj); return nRet; } |