aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-08-26 16:37:06 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-08-26 16:37:06 +0000
commit4f172d82b86011102dcacfcbddc375c1c421eb74 (patch)
treea616d13aa9db93cb9a0757ef883a699eb5367087
parentf0501cc15af1a285bd3cf92f6090850008694bf0 (diff)
downloadpaludis-4f172d82b86011102dcacfcbddc375c1c421eb74.tar.gz
paludis-4f172d82b86011102dcacfcbddc375c1c421eb74.tar.xz
Cleaner handling of libxml2 memory deallocation
-rw-r--r--paludis/qa/metadata_check.cc42
1 files changed, 34 insertions, 8 deletions
diff --git a/paludis/qa/metadata_check.cc b/paludis/qa/metadata_check.cc
index 266c3f8..d44675a 100644
--- a/paludis/qa/metadata_check.cc
+++ b/paludis/qa/metadata_check.cc
@@ -36,7 +36,34 @@ using namespace paludis::qa;
namespace
{
+ template <typename PtrType_>
+ class LibXmlPtrHolder
+ {
+ private:
+ PtrType_ _ptr;
+ void (* _free_func) (PtrType_);
+
+ LibXmlPtrHolder(const LibXmlPtrHolder &);
+ void operator= (const LibXmlPtrHolder &);
+
+ public:
+ LibXmlPtrHolder(PtrType_ ptr, void (* free_func) (PtrType_)) :
+ _ptr(ptr),
+ _free_func(free_func)
+ {
+ }
+ ~LibXmlPtrHolder()
+ {
+ if (0 != _ptr)
+ _free_func(_ptr);
+ }
+
+ operator PtrType_ () const
+ {
+ return _ptr;
+ }
+ };
}
MetadataCheck::MetadataCheck()
@@ -86,27 +113,26 @@ MetadataCheck::operator() (const FSEntry & f) const
"' -- you should remove this file manually before continuing");
}
- xmlParserCtxtPtr xml_parser_context(xmlNewParserCtxt());
- xmlDtdPtr xml_dtd(xmlParseDTD(0, reinterpret_cast<const xmlChar *>(stringify(dtd).c_str())));
+ LibXmlPtrHolder<xmlParserCtxtPtr> xml_parser_context(xmlNewParserCtxt(), &xmlFreeParserCtxt);
+ LibXmlPtrHolder<xmlDtdPtr> xml_dtd(
+ xmlParseDTD(0, reinterpret_cast<const xmlChar *>(stringify(dtd).c_str())), &xmlFreeDtd);
+
if (! xml_dtd)
result << Message(qal_major, "Unable to parse DTD '" + stringify(dtd) + "'");
else
{
- xmlDocPtr xml_doc(xmlCtxtReadFile(xml_parser_context, stringify(f).c_str(), 0, XML_PARSE_DTDLOAD));
+ LibXmlPtrHolder<xmlDocPtr> xml_doc(xmlCtxtReadFile(
+ xml_parser_context, stringify(f).c_str(), 0, XML_PARSE_DTDLOAD), &xmlFreeDoc);
if (! xml_doc)
result << Message(qal_major, "Unable to parse '" + stringify(f) + "'");
else
{
- xmlValidCtxtPtr xml_valid_context(xmlNewValidCtxt());
+ LibXmlPtrHolder<xmlValidCtxtPtr> xml_valid_context(xmlNewValidCtxt(), &xmlFreeValidCtxt);
if (! xmlValidateDtd(xml_valid_context, xml_doc, xml_dtd))
result << Message(qal_major, "Validation of '" + stringify(f) + "' against '"
+ stringify(dtd) + "' failed");
- xmlFreeValidCtxt(xml_valid_context);
- xmlFreeDoc(xml_doc);
}
- xmlFreeDtd(xml_dtd);
}
- xmlFreeParserCtxt(xml_parser_context);
}
return result;