From 1fd8f4747409d8d81fabb3479d146edcb1499e96 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Thu, 2 Mar 2006 17:41:44 +0000 Subject: [PATCH] Manage disposal of new objects when exceptions are thrown. --- xmltooling/io/AbstractXMLObjectUnmarshaller.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp index ad168d1..ab69684 100644 --- a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp @@ -56,17 +56,19 @@ XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool b XT_log.debug("unmarshalling DOM element %s", dname.get()); } - XMLObject* xmlObject = buildXMLObject(element); + auto_ptr xmlObject(buildXMLObject(element)); if (element->hasAttributes()) { - unmarshallAttributes(element, *xmlObject); + unmarshallAttributes(element, *(xmlObject.get())); } - if (element->getTextContent()) { - processElementContent(*xmlObject, element->getTextContent()); + const XMLCh* textContent=element->getTextContent(); + if (textContent && *textContent) { + XT_log.debug("processing element content"); + processElementContent(*(xmlObject.get()), textContent); } - unmarshallChildElements(element, *xmlObject); + unmarshallChildElements(element, *(xmlObject.get())); /* TODO: Signing if (xmlObject instanceof SignableXMLObject) { @@ -74,11 +76,13 @@ XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool b } */ - DOMCachingXMLObject* dc=dynamic_cast(xmlObject); + DOMCachingXMLObject* dc=dynamic_cast(xmlObject.get()); if (dc) dc->setDOM(element,bindDocument); + else if (bindDocument) + throw UnmarshallingException("Unable to bind document to non-DOM caching XMLObject instance."); - return xmlObject; + return xmlObject.release(); } XMLObject* AbstractXMLObjectUnmarshaller::buildXMLObject(const DOMElement* domElement) const @@ -178,7 +182,11 @@ void AbstractXMLObjectUnmarshaller::unmarshallChildElements(const DOMElement* do auto_ptr cname(XMLHelper::getNodeQName(childNode)); XT_log.debug("unmarshalling child element %s", cname->toString().c_str()); } - processChildElement(xmlObject, unmarshaller->unmarshall(static_cast(childNode))); + + // Retain ownership of the unmarshalled child until it's processed by the parent. + auto_ptr childObject(unmarshaller->unmarshall(static_cast(childNode))); + processChildElement(xmlObject, childObject.get()); + childObject.release(); } } } -- 2.1.4