From: Scott Cantor Date: Thu, 2 Mar 2006 17:41:44 +0000 (+0000) Subject: Manage disposal of new objects when exceptions are thrown. X-Git-Tag: 1.0-alpha1~310 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-xmltooling.git;a=commitdiff_plain;h=1fd8f4747409d8d81fabb3479d146edcb1499e96 Manage disposal of new objects when exceptions are thrown. --- 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(); } } }