From ab1272ada43487912f23fd371edbccdf9a5cc69a Mon Sep 17 00:00:00 2001
From: David N Bertoni <dbertoni@apache.org>
Date: Mon, 6 Jun 2005 17:41:39 +0000
Subject: [PATCH] Fixes for Jira issue XERCESC-1368.

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@180348 13f79535-47bb-0310-9956-ffa450edef68
---
 .../framework/LocalFileFormatTarget.cpp       |   7 +-
 src/xercesc/framework/XMLAttDef.cpp           |  12 +-
 src/xercesc/framework/XMLAttr.cpp             |  22 ++-
 src/xercesc/framework/XMLEntityDecl.cpp       |  22 ++-
 src/xercesc/framework/XMLNotationDecl.cpp     |  12 +-
 src/xercesc/framework/psvi/XSValue.cpp        |  36 ++--
 src/xercesc/internal/DGXMLScanner.cpp         | 180 ++++++++----------
 src/xercesc/internal/IGXMLScanner.cpp         | 163 ++++++++--------
 src/xercesc/internal/ReaderMgr.cpp            |  17 +-
 src/xercesc/internal/SGXMLScanner.cpp         | 179 ++++++++---------
 src/xercesc/internal/WFXMLScanner.cpp         | 109 ++++++-----
 src/xercesc/internal/XMLGrammarPoolImpl.cpp   |  30 ++-
 src/xercesc/internal/XMLGrammarPoolImpl.hpp   |   4 +
 src/xercesc/internal/XMLScanner.cpp           | 117 ++++++++----
 src/xercesc/internal/XMLScanner.hpp           |   1 +
 src/xercesc/parsers/AbstractDOMParser.cpp     |  59 +++---
 src/xercesc/parsers/AbstractDOMParser.hpp     |   1 +
 src/xercesc/parsers/DOMBuilderImpl.cpp        |  62 +++---
 src/xercesc/parsers/DOMBuilderImpl.hpp        |   5 +
 src/xercesc/parsers/SAX2XMLReaderImpl.cpp     |  83 ++++----
 src/xercesc/parsers/SAX2XMLReaderImpl.hpp     |   1 +
 src/xercesc/parsers/SAXParser.cpp             |  84 ++++----
 src/xercesc/parsers/SAXParser.hpp             |   1 +
 src/xercesc/parsers/XercesDOMParser.cpp       |  54 +++---
 src/xercesc/parsers/XercesDOMParser.hpp       |   5 +
 src/xercesc/util/Janitor.c                    |  35 +++-
 src/xercesc/util/Janitor.hpp                  |  44 +++++
 src/xercesc/util/QName.cpp                    |  23 ++-
 src/xercesc/util/XMLAbstractDoubleFloat.cpp   |   6 +-
 src/xercesc/util/XMLBigDecimal.cpp            |  33 ++--
 src/xercesc/util/XMLBigInteger.cpp            |   5 +-
 src/xercesc/util/XMLString.cpp                |  46 +++--
 src/xercesc/util/XMLStringTokenizer.cpp       |  22 ++-
 src/xercesc/util/XMLURL.cpp                   |  94 ++++-----
 src/xercesc/util/XMLURL.hpp                   |   2 +-
 src/xercesc/util/XMLUri.cpp                   |  75 +++-----
 src/xercesc/util/regx/BMPattern.cpp           |  21 +-
 src/xercesc/util/regx/RangeTokenMap.cpp       |  26 +--
 src/xercesc/util/regx/RegularExpression.cpp   |  42 ++--
 .../datatype/AnyURIDatatypeValidator.cpp      |  13 +-
 .../datatype/DateDatatypeValidator.cpp        |  10 +-
 .../datatype/DateTimeDatatypeValidator.cpp    |  10 +-
 .../datatype/DayDatatypeValidator.cpp         |  10 +-
 .../datatype/DurationDatatypeValidator.cpp    |  10 +-
 .../datatype/ListDatatypeValidator.cpp        |  10 +-
 .../datatype/MonthDatatypeValidator.cpp       |  10 +-
 .../datatype/MonthDayDatatypeValidator.cpp    |  10 +-
 .../datatype/TimeDatatypeValidator.cpp        |  10 +-
 .../datatype/UnionDatatypeValidator.cpp       |  15 +-
 .../datatype/YearDatatypeValidator.cpp        |  10 +-
 .../datatype/YearMonthDatatypeValidator.cpp   |  10 +-
 .../validators/schema/SchemaGrammar.cpp       |  11 +-
 .../validators/schema/TraverseSchema.cpp      |  10 +-
 .../schema/identity/FieldValueMap.cpp         |  48 +++--
 .../schema/identity/FieldValueMap.hpp         |   5 +
 .../schema/identity/IdentityConstraint.cpp    |  11 +-
 .../identity/IdentityConstraintHandler.cpp    |  11 +-
 .../schema/identity/ValueStoreCache.cpp       |  12 +-
 .../schema/identity/XPathMatcher.cpp          |  20 +-
 .../schema/identity/XPathMatcherStack.cpp     |  23 ++-
 .../schema/identity/XPathMatcherStack.hpp     |   5 +
 .../schema/identity/XercesXPath.cpp           |  11 +-
 62 files changed, 1095 insertions(+), 940 deletions(-)

diff --git a/src/xercesc/framework/LocalFileFormatTarget.cpp b/src/xercesc/framework/LocalFileFormatTarget.cpp
index 87b05351a..1b1199415 100644
--- a/src/xercesc/framework/LocalFileFormatTarget.cpp
+++ b/src/xercesc/framework/LocalFileFormatTarget.cpp
@@ -61,6 +61,7 @@
 #include <xercesc/framework/MemoryManager.hpp>
 #include <xercesc/util/IOException.hpp>
 #include <xercesc/util/OutOfMemoryException.hpp>
+#include <assert.h>
 #include <string.h>
 
 XERCES_CPP_NAMESPACE_BEGIN
@@ -189,10 +190,8 @@ bool LocalFileFormatTarget::insureCapacity(const unsigned int extraNeeded)
     {
         return false;
     }
-    catch (...)
-    {
-        return false;
-    }
+
+    assert(newBuf);
 
     // Copy over the old stuff
     memcpy(newBuf, fDataBuf, fCapacity * sizeof(XMLByte) + 4);
diff --git a/src/xercesc/framework/XMLAttDef.cpp b/src/xercesc/framework/XMLAttDef.cpp
index c01995e12..646a1b9cc 100644
--- a/src/xercesc/framework/XMLAttDef.cpp
+++ b/src/xercesc/framework/XMLAttDef.cpp
@@ -130,6 +130,8 @@ XMLAttDef::XMLAttDef( const XMLAttDef::AttTypes    type
 {
 }
 
+typedef JanitorMemFunCall<XMLAttDef>    CleanupType;
+
 XMLAttDef::XMLAttDef( const XMLCh* const           attrValue
                     , const XMLAttDef::AttTypes    type
                     , const XMLAttDef::DefAttTypes defType
@@ -146,6 +148,8 @@ XMLAttDef::XMLAttDef( const XMLCh* const           attrValue
     , fEnumeration(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLAttDef::cleanUp);
+
     try
     {
         fValue = XMLString::replicate(attrValue, fMemoryManager);
@@ -153,12 +157,12 @@ XMLAttDef::XMLAttDef( const XMLCh* const           attrValue
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 
diff --git a/src/xercesc/framework/XMLAttr.cpp b/src/xercesc/framework/XMLAttr.cpp
index 2bb5bab2b..987317c34 100644
--- a/src/xercesc/framework/XMLAttr.cpp
+++ b/src/xercesc/framework/XMLAttr.cpp
@@ -45,6 +45,8 @@ XMLAttr::XMLAttr(MemoryManager* const manager) :
     fAttName = new (fMemoryManager) QName(fMemoryManager);
 }
 
+typedef JanitorMemFunCall<XMLAttr>  CleanupType;
+
 XMLAttr::XMLAttr(   const   unsigned int        uriId
                     , const XMLCh* const        attrName
                     , const XMLCh* const        attrPrefix
@@ -64,6 +66,8 @@ XMLAttr::XMLAttr(   const   unsigned int        uriId
     , fDatatypeValidator(datatypeValidator)
     , fIsSchemaValidated(isSchema)
 {
+    CleanupType cleanup(this, &XMLAttr::cleanUp);
+
     try
     {
         //
@@ -75,12 +79,12 @@ XMLAttr::XMLAttr(   const   unsigned int        uriId
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 XMLAttr::XMLAttr(   const   unsigned int        uriId
@@ -101,6 +105,8 @@ XMLAttr::XMLAttr(   const   unsigned int        uriId
     , fDatatypeValidator(datatypeValidator)
     , fIsSchemaValidated(isSchema)
 {
+    CleanupType cleanup(this, &XMLAttr::cleanUp);
+
     try
     {
         //  Just call the local setters to set up everything. Too much
@@ -110,12 +116,12 @@ XMLAttr::XMLAttr(   const   unsigned int        uriId
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 
diff --git a/src/xercesc/framework/XMLEntityDecl.cpp b/src/xercesc/framework/XMLEntityDecl.cpp
index 90001b1ab..6ea0a2aa2 100644
--- a/src/xercesc/framework/XMLEntityDecl.cpp
+++ b/src/xercesc/framework/XMLEntityDecl.cpp
@@ -61,6 +61,8 @@ XMLEntityDecl::XMLEntityDecl(const XMLCh* const entName,
     fName = XMLString::replicate(entName, fMemoryManager);
 }
 
+typedef JanitorMemFunCall<XMLEntityDecl>  CleanupType;
+
 XMLEntityDecl::XMLEntityDecl(const  XMLCh* const   entName
                             , const XMLCh* const   value
                             , MemoryManager* const manager) :
@@ -74,6 +76,8 @@ XMLEntityDecl::XMLEntityDecl(const  XMLCh* const   entName
     , fBaseURI(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLEntityDecl::cleanUp);
+
     try
     {
         fValue = XMLString::replicate(value, fMemoryManager);
@@ -81,12 +85,12 @@ XMLEntityDecl::XMLEntityDecl(const  XMLCh* const   entName
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 XMLEntityDecl::XMLEntityDecl(const  XMLCh* const   entName
@@ -102,6 +106,8 @@ XMLEntityDecl::XMLEntityDecl(const  XMLCh* const   entName
     , fBaseURI(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLEntityDecl::cleanUp);
+
     try
     {
         XMLCh dummy[2] = { chNull, chNull };
@@ -111,12 +117,12 @@ XMLEntityDecl::XMLEntityDecl(const  XMLCh* const   entName
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 XMLEntityDecl::~XMLEntityDecl()
diff --git a/src/xercesc/framework/XMLNotationDecl.cpp b/src/xercesc/framework/XMLNotationDecl.cpp
index afb443d9b..fc4052728 100644
--- a/src/xercesc/framework/XMLNotationDecl.cpp
+++ b/src/xercesc/framework/XMLNotationDecl.cpp
@@ -89,6 +89,8 @@ XMLNotationDecl::XMLNotationDecl(MemoryManager* const manager) :
 {
 }
 
+typedef JanitorMemFunCall<XMLNotationDecl>  CleanupType;
+
 XMLNotationDecl::XMLNotationDecl( const XMLCh* const   notName
                                 , const XMLCh* const   pubId
                                 , const XMLCh* const   sysId
@@ -102,6 +104,8 @@ XMLNotationDecl::XMLNotationDecl( const XMLCh* const   notName
     , fBaseURI(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLNotationDecl::cleanUp);
+
     try
     {
         fName = XMLString::replicate(notName, fMemoryManager);
@@ -111,12 +115,12 @@ XMLNotationDecl::XMLNotationDecl( const XMLCh* const   notName
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 XMLNotationDecl::~XMLNotationDecl()
diff --git a/src/xercesc/framework/psvi/XSValue.cpp b/src/xercesc/framework/psvi/XSValue.cpp
index db8b849b2..822ae40d4 100644
--- a/src/xercesc/framework/psvi/XSValue.cpp
+++ b/src/xercesc/framework/psvi/XSValue.cpp
@@ -122,6 +122,7 @@
 #include <xercesc/validators/schema/SchemaSymbols.hpp>
 #include <xercesc/util/OutOfMemoryException.hpp>
 #include <xercesc/util/TransService.hpp>
+#include <xercesc/util/NumberFormatException.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
 
@@ -699,7 +700,7 @@ XSValue::validateNumerics(const XMLCh*         const content
     {
         throw;
     }
-    catch (...)
+    catch (const NumberFormatException&)
     {
         //getActValue()/getCanonical() need to know the failure details
         //if validation is required
@@ -756,7 +757,7 @@ bool XSValue::validateDateTimes(const XMLCh*         const input_content
         return true; //parsing succeed
     }
 
-    catch (SchemaDateTimeException &e)
+    catch (const SchemaDateTimeException &e)
     {       
         status = checkTimeZoneError(datatype, e)? XSValue::st_FODT0003 : st_FOCA0002;
         return false;
@@ -765,7 +766,7 @@ bool XSValue::validateDateTimes(const XMLCh*         const input_content
     {
         throw;
     }
-    catch (...)
+    catch (const NumberFormatException&)
     {
         //getActValue()/getCanonical() need to know the failure details
         //if validation is required
@@ -1067,11 +1068,6 @@ bool XSValue::validateStrings(const XMLCh*         const content
     {
         throw;
     }
-    catch (...)
-    {
-        isValid = false; 
-        status = st_FOCA0002;
-    }
 
     if (isValid == false && status == st_Init) {
         status = st_FOCA0002;
@@ -1090,8 +1086,8 @@ XMLCh* XSValue::getCanRepNumerics(const XMLCh*         const content
     try 
     {
 
-        // All getCanonicalRepresentation does lexcial space validation only
-        // (no range checking), therefore if validation is requied,
+        // getCanonicalRepresentation does lexical space validation only
+        // (no range checking), therefore if validation is required,
         // we need to pass the content to the validate interface for complete checking
         if (toValidate && !validateNumerics(content, datatype, status, manager))
             return 0;
@@ -1131,7 +1127,7 @@ XMLCh* XSValue::getCanRepNumerics(const XMLCh*         const content
     {
         throw;
     }
-    catch (...)
+    catch (const NumberFormatException&)
     {
         status = st_FOCA0002;
         return 0;
@@ -1193,7 +1189,7 @@ XMLCh* XSValue::getCanRepDateTimes(const XMLCh*         const input_content
     {
         throw;
     }
-    catch (...)
+    catch (const NumberFormatException&)
     {
         status = st_FOCA0002;
         return 0;
@@ -1290,11 +1286,7 @@ XMLCh* XSValue::getCanRepStrings(const XMLCh*         const content
     {
         throw;
     }
-    catch (...)
-    {
-        status = st_FOCA0002;
-        return 0;
-    }
+
     return 0;
 }
 
@@ -1477,7 +1469,7 @@ XSValue::getActValNumerics(const XMLCh*         const content
     {
         throw;
     }
-    catch (...)
+    catch (const NumberFormatException&)
     {
         status = st_FOCA0002;
         return 0; 
@@ -1566,7 +1558,7 @@ XSValue::getActValDateTimes(const XMLCh*         const input_content
     {
         throw;
     }
-    catch (...)
+    catch (const NumberFormatException&)
     {
         status = st_FOCA0002;
         return 0; 
@@ -1681,11 +1673,7 @@ XSValue::getActValStrings(const XMLCh*         const content
     {
         throw;
     }
-    catch (...)
-    {
-        status = st_FOCA0002;
-        return 0; 
-    }
+
     return 0; 
 }
 
diff --git a/src/xercesc/internal/DGXMLScanner.cpp b/src/xercesc/internal/DGXMLScanner.cpp
index d3acefd17..426a8e3fd 100644
--- a/src/xercesc/internal/DGXMLScanner.cpp
+++ b/src/xercesc/internal/DGXMLScanner.cpp
@@ -45,6 +45,11 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+
+typedef JanitorMemFunCall<DGXMLScanner> CleanupType;
+typedef JanitorMemFunCall<ReaderMgr>    ReaderMgrResetType;
+
+
 // ---------------------------------------------------------------------------
 //  DGXMLScanner: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -61,29 +66,22 @@ DGXMLScanner::DGXMLScanner(XMLValidator* const valToAdopt
     , fAttDefRegistry(0)
     , fUndeclaredAttrRegistry(0)
 {
+    CleanupType cleanup(this, &DGXMLScanner::cleanUp);
+
     try
     {
         commonInit();
-
-        if (valToAdopt)
-        {
-            if (!valToAdopt->handlesDTD())
-               ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoDTDValidator, fMemoryManager);
-        }
-        else
-        {
-            fValidator = fDTDValidator;
-        }
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 DGXMLScanner::DGXMLScanner( XMLDocumentHandler* const docHandler
@@ -103,29 +101,22 @@ DGXMLScanner::DGXMLScanner( XMLDocumentHandler* const docHandler
     , fAttDefRegistry(0)
     , fUndeclaredAttrRegistry(0)
 {
+    CleanupType cleanup(this, &DGXMLScanner::cleanUp);
+
     try
     {	
         commonInit();
-
-        if (valToAdopt)
-        {
-            if (!valToAdopt->handlesDTD())
-               ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoDTDValidator, fMemoryManager);
-        }
-        else
-        {
-            fValidator = fDTDValidator;
-        }
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 DGXMLScanner::~DGXMLScanner()
@@ -159,6 +150,8 @@ void DGXMLScanner::scanDocument(const InputSource& src)
     //  any previous progressive scan tokens.
     fSequenceId++;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         //  Reset the scanner and its plugged in stuff for a new run. This
@@ -205,9 +198,6 @@ void DGXMLScanner::scanDocument(const InputSource& src)
         // If we have a document handler, then call the end document
         if (fDocHandler)
             fDocHandler->endDocument();
-
-        // Reset the reader manager to close all files, sockets, etc...
-        fReaderMgr.reset();
     }
     //  NOTE:
     //
@@ -216,13 +206,11 @@ void DGXMLScanner::scanDocument(const InputSource& src)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first failure' exception, so fall through
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLException& excToCatch)
     {
@@ -255,26 +243,21 @@ void DGXMLScanner::scanDocument(const InputSource& src)
         }
         catch(const OutOfMemoryException&)
         {
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
-        catch(...)
-        {
-            // Flush the reader manager and rethrow user's error
-            fReaderMgr.reset();
-            throw;
-        }
-
-        // If it returned, then reset the reader manager and fall through
-        fReaderMgr.reset();
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 }
@@ -290,6 +273,8 @@ bool DGXMLScanner::scanNext(XMLPScanToken& token)
     unsigned int orgReader;
     XMLTokens curToken;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     bool retVal = true;
 
     try
@@ -396,15 +381,13 @@ bool DGXMLScanner::scanNext(XMLPScanToken& token)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first failure' exception, so reset and return failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first failure' exception, so return failure
+        retVal = false;
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and reuturn failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first fatal error' type exit, so return failure
+        retVal = false;
     }
     catch(const XMLException& excToCatch)
     {
@@ -437,33 +420,30 @@ bool DGXMLScanner::scanNext(XMLPScanToken& token)
         }
         catch(const OutOfMemoryException&)
         {
-            throw;
-        }
-        catch(...)
-        {
-            // Reset and rethrow user error
-            fReaderMgr.reset();
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
 
-        // Reset and return failure
-        fReaderMgr.reset();
-        return false;
+        retVal = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow original error
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 
-    // If we hit the end, then flush the reader manager
-    if (!retVal)
-        fReaderMgr.reset();
+    // If we are not at the end, release the object that will
+    // reset the ReaderMgr.
+    if (retVal)
+        resetReaderMgr.release();
 
     return retVal;
 }
@@ -1678,6 +1658,8 @@ Grammar* DGXMLScanner::loadGrammar(const   InputSource& src
 {
     Grammar* loadedGrammar = 0;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         fGrammarResolver->cacheGrammarFromParse(false);
@@ -1697,9 +1679,6 @@ Grammar* DGXMLScanner::loadGrammar(const   InputSource& src
         if (grammarType == Grammar::DTDGrammarType) {
             loadedGrammar = loadDTDGrammar(src, toCache);
         }
-
-        // Reset the reader manager to close all files, sockets, etc...
-        fReaderMgr.reset();
     }
     //  NOTE:
     //
@@ -1708,14 +1687,11 @@ Grammar* DGXMLScanner::loadGrammar(const   InputSource& src
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first failure' exception, so fall through
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
-
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLException& excToCatch)
     {
@@ -1747,26 +1723,21 @@ Grammar* DGXMLScanner::loadGrammar(const   InputSource& src
         }
         catch(const OutOfMemoryException&)
         {
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
-        catch(...)
-        {
-            // Flush the reader manager and rethrow user's error
-            fReaderMgr.reset();
-            throw;
-        }
-
-        // If it returned, then reset the reader manager and fall through
-        fReaderMgr.reset();
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 
@@ -1893,7 +1864,6 @@ Grammar* DGXMLScanner::loadDTDGrammar(const InputSource& src,
 //  it redundantly in multiple constructors.
 void DGXMLScanner::commonInit()
 {
-
     //  And we need one for the raw attribute scan. This just stores key/
     //  value string pairs (prior to any processing.)
     fAttrNSList = new (fMemoryManager) ValueVectorOf<XMLAttr*>(8, fMemoryManager);
@@ -1910,6 +1880,16 @@ void DGXMLScanner::commonInit()
     (
         7, false, new (fMemoryManager)HashXMLCh(), fMemoryManager
     );
+
+    if (fValidator)
+    {
+        if (!fValidator->handlesDTD())
+           ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoDTDValidator, fMemoryManager);
+    }
+    else
+    {
+        fValidator = fDTDValidator;
+    }
 }
 
 void DGXMLScanner::cleanUp()
diff --git a/src/xercesc/internal/IGXMLScanner.cpp b/src/xercesc/internal/IGXMLScanner.cpp
index e65af5720..d8fd1fe9b 100644
--- a/src/xercesc/internal/IGXMLScanner.cpp
+++ b/src/xercesc/internal/IGXMLScanner.cpp
@@ -47,6 +47,11 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+
+typedef JanitorMemFunCall<IGXMLScanner> CleanupType;
+typedef JanitorMemFunCall<ReaderMgr>    ReaderMgrResetType;
+
+
 // ---------------------------------------------------------------------------
 //  IGXMLScanner: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -79,23 +84,22 @@ IGXMLScanner::IGXMLScanner( XMLValidator* const  valToAdopt
     , fPSVIElement(0)
     , fErrorStack(0)        
 {
+    CleanupType cleanup(this, &IGXMLScanner::cleanUp);
+
     try
     {
-         commonInit();
-
-         // use fDTDValidator as the default validator
-         if (!valToAdopt)
-             fValidator = fDTDValidator;
+        commonInit();
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 IGXMLScanner::IGXMLScanner( XMLDocumentHandler* const docHandler
@@ -131,23 +135,22 @@ IGXMLScanner::IGXMLScanner( XMLDocumentHandler* const docHandler
     , fPSVIElement(0)
     , fErrorStack(0)       
 {
+    CleanupType cleanup(this, &IGXMLScanner::cleanUp);
+
     try
     {	
         commonInit();
-
-        //use fDTDValidator as the default validator
-        if (!valToAdopt)
-            fValidator = fDTDValidator;
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 IGXMLScanner::~IGXMLScanner()
@@ -181,6 +184,8 @@ void IGXMLScanner::scanDocument(const InputSource& src)
     //  any previous progressive scan tokens.
     fSequenceId++;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         //  Reset the scanner and its plugged in stuff for a new run. This
@@ -230,9 +235,6 @@ void IGXMLScanner::scanDocument(const InputSource& src)
 
         //cargill debug:
         //fGrammarResolver->getXSModel();
-
-        // Reset the reader manager to close all files, sockets, etc...
-        fReaderMgr.reset();
     }
     //  NOTE:
     //
@@ -241,13 +243,11 @@ void IGXMLScanner::scanDocument(const InputSource& src)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first failure' exception, so fall through
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLException& excToCatch)
     {
@@ -280,26 +280,21 @@ void IGXMLScanner::scanDocument(const InputSource& src)
         }
         catch(const OutOfMemoryException&)
         {
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
-        catch(...)
-        {
-            // Flush the reader manager and rethrow user's error
-            fReaderMgr.reset();
-            throw;
-        }
-
-        // If it returned, then reset the reader manager and fall through
-        fReaderMgr.reset();
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 }
@@ -315,6 +310,8 @@ bool IGXMLScanner::scanNext(XMLPScanToken& token)
     unsigned int orgReader;
     XMLTokens curToken;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     bool retVal = true;
 
     try
@@ -427,15 +424,13 @@ bool IGXMLScanner::scanNext(XMLPScanToken& token)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first failure' exception, so reset and return failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first failure' exception so return failure
+        retVal = false;
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and reuturn failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first fatal error' type exit, so return failure
+        retVal = false;
     }
     catch(const XMLException& excToCatch)
     {
@@ -468,33 +463,30 @@ bool IGXMLScanner::scanNext(XMLPScanToken& token)
         }
         catch(const OutOfMemoryException&)
         {
-            throw;
-        }
-        catch(...)
-        {
-            // Reset and rethrow user error
-            fReaderMgr.reset();
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
 
-        // Reset and return failure
-        fReaderMgr.reset();
-        return false;
+        retVal = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow original error
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 
-    // If we hit the end, then flush the reader manager
-    if (!retVal)
-        fReaderMgr.reset();
+    // If we are not at the end, release the object that will
+    // reset the ReaderMgr.
+    if (retVal)
+        resetReaderMgr.release();
 
     return retVal;
 }
@@ -552,6 +544,10 @@ void IGXMLScanner::commonInit()
         7, false, new (fMemoryManager)HashXMLCh(), fMemoryManager
     );
     fPSVIAttrList = new (fMemoryManager) PSVIAttributeList(fMemoryManager);
+
+    // use fDTDValidator as the default validator
+    if (!fValidator)
+        fValidator = fDTDValidator;
 }
 
 void IGXMLScanner::cleanUp()
@@ -2945,6 +2941,8 @@ Grammar* IGXMLScanner::loadGrammar(const   InputSource& src
 {
     Grammar* loadedGrammar = 0;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         fGrammarResolver->cacheGrammarFromParse(false);
@@ -2971,9 +2969,6 @@ Grammar* IGXMLScanner::loadGrammar(const   InputSource& src
         else if (grammarType == Grammar::DTDGrammarType) {
             loadedGrammar = loadDTDGrammar(src, toCache);
         }
-
-        // Reset the reader manager to close all files, sockets, etc...
-        fReaderMgr.reset();
     }
     //  NOTE:
     //
@@ -2982,14 +2977,11 @@ Grammar* IGXMLScanner::loadGrammar(const   InputSource& src
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
-
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLException& excToCatch)
     {
@@ -3021,26 +3013,21 @@ Grammar* IGXMLScanner::loadGrammar(const   InputSource& src
         }
         catch(const OutOfMemoryException&)
         {
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
-        catch(...)
-        {
-            // Flush the reader manager and rethrow user's error
-            fReaderMgr.reset();
-            throw;
-        }
-
-        // If it returned, then reset the reader manager and fall through
-        fReaderMgr.reset();
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 
diff --git a/src/xercesc/internal/ReaderMgr.cpp b/src/xercesc/internal/ReaderMgr.cpp
index a89ca5b29..1eccfc6d5 100644
--- a/src/xercesc/internal/ReaderMgr.cpp
+++ b/src/xercesc/internal/ReaderMgr.cpp
@@ -387,6 +387,8 @@ XMLReader* ReaderMgr::createReader( const   InputSource&        src
     if (!newStream)
         return 0;
 
+    Janitor<BinInputStream>   streamJanitor(newStream);
+
     //
     //  Create a new reader and return it. If the source has an encoding that
     //  it wants to force, then we call the constructor that does that.
@@ -438,19 +440,14 @@ XMLReader* ReaderMgr::createReader( const   InputSource&        src
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...) //NetAccessorException&
-    {
-        delete newStream;
+        streamJanitor.release();
+
         throw;
     }
 
-    // If it failed for any reason, then return zero.
-    if (!retVal) {
-        delete newStream;
-        return 0;
-    }
+    assert(retVal);
+
+    streamJanitor.release();
 
     // Set the next available reader number on this reader
     retVal->setReaderNum(fNextReaderNum++);
diff --git a/src/xercesc/internal/SGXMLScanner.cpp b/src/xercesc/internal/SGXMLScanner.cpp
index c1a4cff67..c04cc8079 100644
--- a/src/xercesc/internal/SGXMLScanner.cpp
+++ b/src/xercesc/internal/SGXMLScanner.cpp
@@ -51,6 +51,11 @@ XERCES_CPP_NAMESPACE_BEGIN
 
 inline XMLAttDefList& getAttDefList(ComplexTypeInfo* currType, XMLElementDecl* elemDecl);
 
+
+typedef JanitorMemFunCall<SGXMLScanner> CleanupType;
+typedef JanitorMemFunCall<ReaderMgr>    ReaderMgrResetType;
+
+
 // ---------------------------------------------------------------------------
 //  SGXMLScanner: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -80,29 +85,22 @@ SGXMLScanner::SGXMLScanner( XMLValidator* const valToAdopt
     , fPSVIElement(0)
     , fErrorStack(0)       
 {
+    CleanupType cleanup(this, &SGXMLScanner::cleanUp);
+
     try
     {
          commonInit();
-
-         if (valToAdopt)
-         {
-             if (!valToAdopt->handlesSchema())
-                ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoSchemaValidator, fMemoryManager);
-         }
-         else
-         {
-             fValidator = fSchemaValidator;
-         }
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 SGXMLScanner::SGXMLScanner( XMLDocumentHandler* const docHandler
@@ -135,29 +133,22 @@ SGXMLScanner::SGXMLScanner( XMLDocumentHandler* const docHandler
     , fPSVIElement(0)
     , fErrorStack(0)        
 {
+    CleanupType cleanup(this, &SGXMLScanner::cleanUp);
+
     try
     {	
         commonInit();
-
-         if (valToAdopt)
-         {
-             if (!valToAdopt->handlesSchema())
-                ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoSchemaValidator, fMemoryManager);
-         }
-         else
-         {
-             fValidator = fSchemaValidator;
-         }
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 SGXMLScanner::~SGXMLScanner()
@@ -187,6 +178,8 @@ void SGXMLScanner::scanDocument(const InputSource& src)
     //  any previous progressive scan tokens.
     fSequenceId++;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         //  Reset the scanner and its plugged in stuff for a new run. This
@@ -233,9 +226,6 @@ void SGXMLScanner::scanDocument(const InputSource& src)
         // If we have a document handler, then call the end document
         if (fDocHandler)
             fDocHandler->endDocument();
-
-        // Reset the reader manager to close all files, sockets, etc...
-        fReaderMgr.reset();
     }
     //  NOTE:
     //
@@ -244,13 +234,11 @@ void SGXMLScanner::scanDocument(const InputSource& src)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first failure' exception, so fall through
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLException& excToCatch)
     {
@@ -283,26 +271,21 @@ void SGXMLScanner::scanDocument(const InputSource& src)
         }
         catch(const OutOfMemoryException&)
         {
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
-        catch(...)
-        {
-            // Flush the reader manager and rethrow user's error
-            fReaderMgr.reset();
-            throw;
-        }
-
-        // If it returned, then reset the reader manager and fall through
-        fReaderMgr.reset();
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 }
@@ -318,6 +301,8 @@ bool SGXMLScanner::scanNext(XMLPScanToken& token)
     unsigned int orgReader;
     XMLTokens curToken;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     bool retVal = true;
 
     try
@@ -427,15 +412,13 @@ bool SGXMLScanner::scanNext(XMLPScanToken& token)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first failure' exception, so reset and return failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first failure' exception, so return failure
+        retVal = false;
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and reuturn failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first fatal error' type exit, so return failure
+        retVal = false;
     }
     catch(const XMLException& excToCatch)
     {
@@ -468,33 +451,30 @@ bool SGXMLScanner::scanNext(XMLPScanToken& token)
         }
         catch(const OutOfMemoryException&)
         {
-            throw;
-        }
-        catch(...)
-        {
-            // Reset and rethrow user error
-            fReaderMgr.reset();
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
 
-        // Reset and return failure
-        fReaderMgr.reset();
-        return false;
+        retVal = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow original error
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 
-    // If we hit the end, then flush the reader manager
-    if (!retVal)
-        fReaderMgr.reset();
+    // If we are not at the end, release the object that will
+    // reset the ReaderMgr.
+    if (retVal)
+        resetReaderMgr.release();
 
     return retVal;
 }
@@ -1890,6 +1870,8 @@ Grammar* SGXMLScanner::loadGrammar(const   InputSource& src
 {
     Grammar* loadedGrammar = 0;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         fGrammarResolver->cacheGrammarFromParse(false);
@@ -1913,9 +1895,6 @@ Grammar* SGXMLScanner::loadGrammar(const   InputSource& src
         if (grammarType == Grammar::SchemaGrammarType) {
             loadedGrammar = loadXMLSchemaGrammar(src, toCache);
         }
-
-        // Reset the reader manager to close all files, sockets, etc...
-        fReaderMgr.reset();
     }
     //  NOTE:
     //
@@ -1924,14 +1903,11 @@ Grammar* SGXMLScanner::loadGrammar(const   InputSource& src
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first failure' exception, so fall through
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
-
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLException& excToCatch)
     {
@@ -1963,26 +1939,21 @@ Grammar* SGXMLScanner::loadGrammar(const   InputSource& src
         }
         catch(const OutOfMemoryException&)
         {
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
-        catch(...)
-        {
-            // Flush the reader manager and rethrow user's error
-            fReaderMgr.reset();
-            throw;
-        }
-
-        // If it returned, then reset the reader manager and fall through
-        fReaderMgr.reset();
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 
@@ -2035,6 +2006,16 @@ void SGXMLScanner::commonInit()
         7, false, new (fMemoryManager)HashXMLCh(), fMemoryManager
     );
     fPSVIAttrList = new (fMemoryManager) PSVIAttributeList(fMemoryManager);
+
+    if (fValidator)
+    {
+        if (!fValidator->handlesSchema())
+            ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoSchemaValidator, fMemoryManager);
+    }
+    else
+    {
+        fValidator = fSchemaValidator;
+    }
 }
 
 void SGXMLScanner::cleanUp()
diff --git a/src/xercesc/internal/WFXMLScanner.cpp b/src/xercesc/internal/WFXMLScanner.cpp
index c466488b4..6303f1247 100644
--- a/src/xercesc/internal/WFXMLScanner.cpp
+++ b/src/xercesc/internal/WFXMLScanner.cpp
@@ -39,6 +39,12 @@ XERCES_CPP_NAMESPACE_BEGIN
 // ---------------------------------------------------------------------------
 //  WFXMLScanner: Constructors and Destructor
 // ---------------------------------------------------------------------------
+
+
+typedef JanitorMemFunCall<WFXMLScanner> CleanupType;
+typedef JanitorMemFunCall<ReaderMgr>    ReaderMgrResetType;
+
+
 WFXMLScanner::WFXMLScanner( XMLValidator* const  valToAdopt
                           , GrammarResolver* const grammarResolver
                           , MemoryManager* const manager) :
@@ -51,19 +57,22 @@ WFXMLScanner::WFXMLScanner( XMLValidator* const  valToAdopt
     , fAttrNSList(0)
     , fElementLookup(0)
 {
+    CleanupType cleanup(this, &WFXMLScanner::cleanUp);
+
     try
     {
         commonInit();
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 WFXMLScanner::WFXMLScanner( XMLDocumentHandler* const docHandler
@@ -82,19 +91,22 @@ WFXMLScanner::WFXMLScanner( XMLDocumentHandler* const docHandler
     , fAttrNSList(0)
     , fElementLookup(0)
 {
+    CleanupType cleanup(this, &WFXMLScanner::cleanUp);
+
     try
     {	
         commonInit();
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 WFXMLScanner::~WFXMLScanner()
@@ -124,6 +136,8 @@ void WFXMLScanner::scanDocument(const InputSource& src)
     //  any previous progressive scan tokens.
     fSequenceId++;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         //  Reset the scanner and its plugged in stuff for a new run. This
@@ -159,9 +173,6 @@ void WFXMLScanner::scanDocument(const InputSource& src)
         // If we have a document handler, then call the end document
         if (fDocHandler)
             fDocHandler->endDocument();
-
-        // Reset the reader manager to close all files, sockets, etc...
-        fReaderMgr.reset();
     }
     //  NOTE:
     //
@@ -170,13 +181,11 @@ void WFXMLScanner::scanDocument(const InputSource& src)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first failure' exception, so fall through
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and fall through
-        fReaderMgr.reset();
+        // This is a 'first fatal error' type exit, so fall through
     }
     catch(const XMLException& excToCatch)
     {
@@ -209,26 +218,21 @@ void WFXMLScanner::scanDocument(const InputSource& src)
         }
         catch(const OutOfMemoryException&)
         {
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
-        catch(...)
-        {
-            // Flush the reader manager and rethrow user's error
-            fReaderMgr.reset();
-            throw;
-        }
-
-        // If it returned, then reset the reader manager and fall through
-        fReaderMgr.reset();
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 }
@@ -245,6 +249,8 @@ bool WFXMLScanner::scanNext(XMLPScanToken& token)
     XMLTokens curToken;
     bool retVal = true;
 
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
     try
     {
         while (true)
@@ -341,15 +347,13 @@ bool WFXMLScanner::scanNext(XMLPScanToken& token)
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first failure' exception, so reset and return failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first failure' exception, so return failure
+        retVal = false;
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and reuturn failure
-        fReaderMgr.reset();
-        return false;
+        // This is a 'first fatal error' type exit, so return failure
+        retVal = false;
     }
     catch(const XMLException& excToCatch)
     {
@@ -382,33 +386,26 @@ bool WFXMLScanner::scanNext(XMLPScanToken& token)
         }
         catch(const OutOfMemoryException&)
         {
-            throw;
-        }
-        catch(...)
-        {
-            // Reset and rethrow user error
-            fReaderMgr.reset();
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
 
-        // Reset and return failure
-        fReaderMgr.reset();
-        return false;
+        // Return failure
+        retVal = false;
     }
     catch(const OutOfMemoryException&)
     {
         throw;
     }
-    catch(...)
-    {
-        // Reset and rethrow original error
-        fReaderMgr.reset();
-        throw;
-    }
 
-    // If we hit the end, then flush the reader manager
-    if (!retVal)
-        fReaderMgr.reset();
+    // If we are not at the end, release the object that will
+    // reset the ReaderMgr.
+    if (retVal)
+        resetReaderMgr.release();
 
     return retVal;
 }
diff --git a/src/xercesc/internal/XMLGrammarPoolImpl.cpp b/src/xercesc/internal/XMLGrammarPoolImpl.cpp
index 0a8fda99b..71e6d4ad1 100644
--- a/src/xercesc/internal/XMLGrammarPoolImpl.cpp
+++ b/src/xercesc/internal/XMLGrammarPoolImpl.cpp
@@ -386,7 +386,11 @@ void XMLGrammarPoolImpl::deserializeGrammars(BinInputStream* const binIn)
     {
         ThrowXMLwithMemMgr(XSerializationException, XMLExcepts::XSer_GrammarPool_NotEmpty, memMgr);
     }
-    
+
+    // This object will take care of cleaning up if an exception is
+    // thrown during deserialization.
+    JanitorMemFunCall<XMLGrammarPoolImpl>   cleanup(this, &XMLGrammarPoolImpl::cleanUp);
+
     try 
     {
         XSerializeEngine  serEng(binIn, this);
@@ -426,14 +430,17 @@ void XMLGrammarPoolImpl::deserializeGrammars(BinInputStream* const binIn)
     }
     catch(const OutOfMemoryException&)
     {
+        // This is a special case, because we don't want
+        // to execute cleanup code on out-of-memory
+        // conditions.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        fLocked = false; // need to unset it so we can clean it out..
-        clear();  //clear all deserialized grammars
-        throw;
-    }
+
+    // Everything is OK, so we can release the cleanup object.
+    cleanup.release();
+
     if (fLocked) 
     {
         createXSModel();              
@@ -441,4 +448,13 @@ void XMLGrammarPoolImpl::deserializeGrammars(BinInputStream* const binIn)
 }
 
 
+void
+XMLGrammarPoolImpl::cleanUp()
+{
+    fLocked = false;
+
+    clear();
+}
+
+
 XERCES_CPP_NAMESPACE_END
diff --git a/src/xercesc/internal/XMLGrammarPoolImpl.hpp b/src/xercesc/internal/XMLGrammarPoolImpl.hpp
index 73c97f982..7fab1a584 100644
--- a/src/xercesc/internal/XMLGrammarPoolImpl.hpp
+++ b/src/xercesc/internal/XMLGrammarPoolImpl.hpp
@@ -325,6 +325,10 @@ public :
 private:
 
     virtual void    createXSModel();
+
+    void
+    cleanUp();
+
     // -----------------------------------------------------------------------
     /** name  Unimplemented copy constructor and operator= */
     // -----------------------------------------------------------------------
diff --git a/src/xercesc/internal/XMLScanner.cpp b/src/xercesc/internal/XMLScanner.cpp
index f15f2dcb8..60f53afb8 100644
--- a/src/xercesc/internal/XMLScanner.cpp
+++ b/src/xercesc/internal/XMLScanner.cpp
@@ -134,6 +134,11 @@ void XMLInitializer::initializeScannerMsgLoader()
     }
 }
 
+
+typedef JanitorMemFunCall<XMLScanner>   CleanupType;
+typedef JanitorMemFunCall<ReaderMgr>    ReaderMgrResetType;
+
+
 // ---------------------------------------------------------------------------
 //  XMLScanner: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -212,12 +217,22 @@ XMLScanner::XMLScanner(XMLValidator* const valToAdopt,
     , fWSNormalizeBuf(1023, manager)
     , fElemStack(manager)   
 {
-   commonInit();
+    CleanupType cleanup(this, &XMLScanner::cleanUp);
 
-   if (fValidator) {
-       fValidatorFromUser = true;
-       initValidator(fValidator);
-   }
+    try
+    {
+        commonInit();
+    }
+    catch(const OutOfMemoryException&)
+    {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
+        throw;
+    }
+
+    cleanup.release();
 }
 
 XMLScanner::XMLScanner( XMLDocumentHandler* const  docHandler
@@ -300,28 +315,27 @@ XMLScanner::XMLScanner( XMLDocumentHandler* const  docHandler
     , fWSNormalizeBuf(1023, manager)
     , fElemStack(manager)
 {
-   commonInit();
+    CleanupType cleanup(this, &XMLScanner::cleanUp);
 
-   if (valToAdopt){
-       fValidatorFromUser = true;
-       initValidator(fValidator);
-   }
+    try
+    {
+        commonInit();
+    }
+    catch(const OutOfMemoryException&)
+    {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
+        throw;
+    }
+
+    cleanup.release();
 }
 
 XMLScanner::~XMLScanner()
 {
-    delete fAttrList;
-    delete fAttrDupChkRegistry;
-    delete fValidationContext;
-    fMemoryManager->deallocate(fRootElemName);//delete [] fRootElemName;
-    fMemoryManager->deallocate(fExternalSchemaLocation);//delete [] fExternalSchemaLocation;
-    fMemoryManager->deallocate(fExternalNoNamespaceSchemaLocation);//delete [] fExternalNoNamespaceSchemaLocation;
-    // delete fUIntPool
-    for (unsigned int i=0; i<=fUIntPoolRow; i++)
-    {
-        fMemoryManager->deallocate(fUIntPool[i]);
-    }
-    fMemoryManager->deallocate(fUIntPool);
+    cleanUp();
 }
 
 
@@ -567,7 +581,9 @@ bool XMLScanner::scanFirst( const   InputSource&    src
     //  any previous tokens we've returned.
     fSequenceId++;
 
-    // Reset the scanner and its plugged in stuff for a new run.  This
+    ReaderMgrResetType  resetReaderMgr(&fReaderMgr, &ReaderMgr::reset);
+
+   // Reset the scanner and its plugged in stuff for a new run.  This
     // resets all the data structures, creates the initial reader and
     // pushes it on the stack, and sets up the base document path
     scanReset(src);
@@ -597,14 +613,12 @@ bool XMLScanner::scanFirst( const   InputSource&    src
     //  to find out the position in the XML source of the error.
     catch(const XMLErrs::Codes)
     {
-        // This is a 'first failure' exception so reset and return a failure
-        fReaderMgr.reset();
+        // This is a 'first failure' exception so return failure
         return false;
     }
     catch(const XMLValid::Codes)
     {
-        // This is a 'first fatal error' type exit, so reset and reuturn failure
-        fReaderMgr.reset();
+        // This is a 'first fatal error' type exit, return failure
         return false;
     }
     catch(const XMLException& excToCatch)
@@ -638,32 +652,33 @@ bool XMLScanner::scanFirst( const   InputSource&    src
         }
         catch(const OutOfMemoryException&)
         {
-            throw;
-        }
-        catch(...)
-        {
-            // Reset and rethrow the user error
-            fReaderMgr.reset();
+            // This is a special case for out-of-memory
+            // conditions, because resetting the ReaderMgr
+            // can be problematic.
+            resetReaderMgr.release();
+
             throw;
         }
 
-        // Reset and return a failure
-        fReaderMgr.reset();
         return false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        // Reset and rethrow original error
-        fReaderMgr.reset();
+        // This is a special case for out-of-memory
+        // conditions, because resetting the ReaderMgr
+        // can be problematic.
+        resetReaderMgr.release();
+
         throw;
     }
 
     // Fill in the caller's token to make it legal and return success
     toFill.set(fScannerId, fSequenceId);
+
+    // Release the object that will reset the ReaderMgr, since there's
+    // more to scan.
+    resetReaderMgr.release();
+
     return true;
 }
 
@@ -747,8 +762,28 @@ void XMLScanner::commonInit()
 
     // Register self as handler for XMLBufferFull events on the CDATA buffer
     fCDataBuf.setFullHandler(this, fBufferSize);
+
+   if (fValidator) {
+       fValidatorFromUser = true;
+       initValidator(fValidator);
+   }
 }
 
+void XMLScanner::cleanUp()
+{
+    delete fAttrList;
+    delete fAttrDupChkRegistry;
+    delete fValidationContext;
+    fMemoryManager->deallocate(fRootElemName);//delete [] fRootElemName;
+    fMemoryManager->deallocate(fExternalSchemaLocation);//delete [] fExternalSchemaLocation;
+    fMemoryManager->deallocate(fExternalNoNamespaceSchemaLocation);//delete [] fExternalNoNamespaceSchemaLocation;
+    // delete fUIntPool
+    for (unsigned int i=0; i<=fUIntPoolRow; i++)
+    {
+        fMemoryManager->deallocate(fUIntPool[i]);
+    }
+    fMemoryManager->deallocate(fUIntPool);
+}
 
 void XMLScanner::initValidator(XMLValidator* theValidator) {
 
diff --git a/src/xercesc/internal/XMLScanner.hpp b/src/xercesc/internal/XMLScanner.hpp
index af024f297..184ae46b6 100644
--- a/src/xercesc/internal/XMLScanner.hpp
+++ b/src/xercesc/internal/XMLScanner.hpp
@@ -1102,6 +1102,7 @@ private :
     //  Private helper methods
     // -----------------------------------------------------------------------
     void commonInit();
+    void cleanUp();
 
     // -----------------------------------------------------------------------
     //  Private scanning methods
diff --git a/src/xercesc/parsers/AbstractDOMParser.cpp b/src/xercesc/parsers/AbstractDOMParser.cpp
index d73a28620..29d34c6bd 100644
--- a/src/xercesc/parsers/AbstractDOMParser.cpp
+++ b/src/xercesc/parsers/AbstractDOMParser.cpp
@@ -69,6 +69,12 @@ XERCES_CPP_NAMESPACE_BEGIN
 // ---------------------------------------------------------------------------
 //  AbstractDOMParser: Constructors and Destructor
 // ---------------------------------------------------------------------------
+
+
+typedef JanitorMemFunCall<AbstractDOMParser>    CleanupType;
+typedef JanitorMemFunCall<AbstractDOMParser>    ResetInProgressType;
+
+
 AbstractDOMParser::AbstractDOMParser( XMLValidator* const   valToAdopt
                                     , MemoryManager* const  manager
                                     , XMLGrammarPool* const gramPool) :
@@ -98,19 +104,22 @@ AbstractDOMParser::AbstractDOMParser( XMLValidator* const   valToAdopt
 , fInternalSubset(fBufMgr.bidOnBuffer())
 , fPSVIHandler(0)
 {
+    CleanupType cleanup(this, &AbstractDOMParser::cleanUp);
+
     try
     {
         initialize();
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-       cleanUp();
-       throw;
-    }
+
+    cleanup.release();
 }
 
 
@@ -185,6 +194,12 @@ void AbstractDOMParser::reset()
 }
 
 
+void AbstractDOMParser::resetInProgress()
+{
+    fParseInProgress = false;
+}
+
+
 void AbstractDOMParser::resetPool()
 {
     //  We cannot enter here while a regular parse is in progress.
@@ -492,21 +507,19 @@ void AbstractDOMParser::parse(const InputSource& source)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &AbstractDOMParser::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(source);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
+        resetInProgress.release();
+
         throw;
     }    
-    catch(...)
-    {
-        fParseInProgress = false;
-        throw;
-    }
 }
 
 void AbstractDOMParser::parse(const XMLCh* const systemId)
@@ -515,21 +528,19 @@ void AbstractDOMParser::parse(const XMLCh* const systemId)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &AbstractDOMParser::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(systemId);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
+        resetInProgress.release();
+
         throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
-        throw;
-    }
+    }    
 }
 
 void AbstractDOMParser::parse(const char* const systemId)
@@ -538,21 +549,19 @@ void AbstractDOMParser::parse(const char* const systemId)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &AbstractDOMParser::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(systemId);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
+        resetInProgress.release();
+
         throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
-        throw;
-    }
+    }    
 }
 
 
diff --git a/src/xercesc/parsers/AbstractDOMParser.hpp b/src/xercesc/parsers/AbstractDOMParser.hpp
index 5e7174bb7..4d10455fa 100644
--- a/src/xercesc/parsers/AbstractDOMParser.hpp
+++ b/src/xercesc/parsers/AbstractDOMParser.hpp
@@ -1648,6 +1648,7 @@ private :
     // -----------------------------------------------------------------------
     void initialize();
     void cleanUp();
+    void resetInProgress();
 
     // -----------------------------------------------------------------------
     //  Unimplemented constructors and operators
diff --git a/src/xercesc/parsers/DOMBuilderImpl.cpp b/src/xercesc/parsers/DOMBuilderImpl.cpp
index 1000f003b..bae594b74 100644
--- a/src/xercesc/parsers/DOMBuilderImpl.cpp
+++ b/src/xercesc/parsers/DOMBuilderImpl.cpp
@@ -605,6 +605,8 @@ DOMBuilderImpl::resolveEntity( XMLResourceIdentifier* resourceIdentifier )
     return 0;
 }
 
+typedef JanitorMemFunCall<DOMBuilderImpl>    ResetParseType;
+
 // ---------------------------------------------------------------------------
 //  DOMBuilderImpl: Grammar preparsing methods
 // ---------------------------------------------------------------------------
@@ -616,7 +618,10 @@ Grammar* DOMBuilderImpl::loadGrammar(const char* const systemId,
     if (getParseInProgress())
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetParseType  resetParse(this, &DOMBuilderImpl::resetParse);
+
 	Grammar* grammar = 0;
+
     try
     {
         setParseInProgress(true);
@@ -628,20 +633,11 @@ Grammar* DOMBuilderImpl::loadGrammar(const char* const systemId,
         DOMDocument* doc = adoptDocument();
         if (doc)
             doc->release();
-        
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
+        resetParse.release();
+
         throw;
     }
 
@@ -656,7 +652,10 @@ Grammar* DOMBuilderImpl::loadGrammar(const XMLCh* const systemId,
     if (getParseInProgress())
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
-    Grammar* grammar = 0;
+    ResetParseType  resetParse(this, &DOMBuilderImpl::resetParse);
+
+	Grammar* grammar = 0;
+
     try
     {
         setParseInProgress(true);
@@ -668,20 +667,11 @@ Grammar* DOMBuilderImpl::loadGrammar(const XMLCh* const systemId,
         DOMDocument* doc = adoptDocument();
         if (doc)
             doc->release();
-
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
+        resetParse.release();
+
         throw;
     }
 
@@ -696,7 +686,10 @@ Grammar* DOMBuilderImpl::loadGrammar(const DOMInputSource& source,
     if (getParseInProgress())
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetParseType  resetParse(this, &DOMBuilderImpl::resetParse);
+
     Grammar* grammar = 0;
+
     try
     {
         Wrapper4DOMInputSource isWrapper((DOMInputSource*) &source, false, getMemoryManager());
@@ -710,20 +703,11 @@ Grammar* DOMBuilderImpl::loadGrammar(const DOMInputSource& source,
         DOMDocument* doc = adoptDocument();
         if (doc)
             doc->release();
-
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
+        resetParse.release();
+
         throw;
     }
 
@@ -735,6 +719,16 @@ void DOMBuilderImpl::resetCachedGrammarPool()
     getGrammarResolver()->resetCachedGrammar();
 }
 
+void DOMBuilderImpl::resetParse()
+{
+    if (getScanner()->getDocTypeHandler() == 0)
+    {
+        getScanner()->setDocTypeHandler(this);
+    }
+
+    setParseInProgress(false);
+}
+
 Grammar* DOMBuilderImpl::getGrammar(const XMLCh* const nameSpaceKey) const
 {
     return getGrammarResolver()->getGrammar(nameSpaceKey);
diff --git a/src/xercesc/parsers/DOMBuilderImpl.hpp b/src/xercesc/parsers/DOMBuilderImpl.hpp
index bb80c9d64..5be204516 100644
--- a/src/xercesc/parsers/DOMBuilderImpl.hpp
+++ b/src/xercesc/parsers/DOMBuilderImpl.hpp
@@ -836,6 +836,11 @@ public :
 
 
 private :
+    // -----------------------------------------------------------------------
+    //  Initialize/Cleanup methods
+    // -----------------------------------------------------------------------
+    void resetParse();
+
     // -----------------------------------------------------------------------
     //  Private data members
     //
diff --git a/src/xercesc/parsers/SAX2XMLReaderImpl.cpp b/src/xercesc/parsers/SAX2XMLReaderImpl.cpp
index a1484f28f..47d8f0abb 100644
--- a/src/xercesc/parsers/SAX2XMLReaderImpl.cpp
+++ b/src/xercesc/parsers/SAX2XMLReaderImpl.cpp
@@ -332,6 +332,12 @@ const XMLCh gDTDEntityStr[] =
     chOpenSquare, chLatin_d, chLatin_t, chLatin_d, chCloseSquare, chNull
 };
 
+
+
+typedef JanitorMemFunCall<SAX2XMLReaderImpl>    CleanupType;
+typedef JanitorMemFunCall<SAX2XMLReaderImpl>    ResetInProgressType;
+
+
 SAX2XMLReaderImpl::SAX2XMLReaderImpl(MemoryManager* const  manager
                                    , XMLGrammarPool* const gramPool):
 
@@ -363,19 +369,22 @@ SAX2XMLReaderImpl::SAX2XMLReaderImpl(MemoryManager* const  manager
     , fMemoryManager(manager)
     , fGrammarPool(gramPool)
 {
+    CleanupType cleanup(this, &SAX2XMLReaderImpl::cleanUp);
+
     try
     {
         initialize();
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 SAX2XMLReaderImpl::~SAX2XMLReaderImpl()
@@ -664,19 +673,17 @@ void SAX2XMLReaderImpl::parse (const   InputSource&    source)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAX2XMLReaderImpl::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(source);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 }
@@ -687,19 +694,17 @@ void SAX2XMLReaderImpl::parse (const   XMLCh* const    systemId)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAX2XMLReaderImpl::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(systemId);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 }
@@ -710,6 +715,8 @@ void SAX2XMLReaderImpl::parse (const   char* const     systemId)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAX2XMLReaderImpl::resetInProgress);
+
     try
     {
         fParseInProgress = true;
@@ -718,11 +725,8 @@ void SAX2XMLReaderImpl::parse (const   char* const     systemId)
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 }
@@ -1746,20 +1750,18 @@ Grammar* SAX2XMLReaderImpl::loadGrammar(const char* const systemId,
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAX2XMLReaderImpl::resetInProgress);
+
     Grammar* grammar = 0;
     try
     {
         fParseInProgress = true;
         grammar = fScanner->loadGrammar(systemId, grammarType, toCache);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 
@@ -1774,20 +1776,18 @@ Grammar* SAX2XMLReaderImpl::loadGrammar(const XMLCh* const systemId,
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAX2XMLReaderImpl::resetInProgress);
+
     Grammar* grammar = 0;
     try
     {
         fParseInProgress = true;
         grammar = fScanner->loadGrammar(systemId, grammarType, toCache);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 
@@ -1802,26 +1802,29 @@ Grammar* SAX2XMLReaderImpl::loadGrammar(const InputSource& source,
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAX2XMLReaderImpl::resetInProgress);
+
     Grammar* grammar = 0;
     try
     {
         fParseInProgress = true;
         grammar = fScanner->loadGrammar(source, grammarType, toCache);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 
     return grammar;
 }
 
+void SAX2XMLReaderImpl::resetInProgress()
+{
+    fParseInProgress = false;
+}
+
 void SAX2XMLReaderImpl::resetCachedGrammarPool()
 {
     fGrammarResolver->resetCachedGrammar();
diff --git a/src/xercesc/parsers/SAX2XMLReaderImpl.hpp b/src/xercesc/parsers/SAX2XMLReaderImpl.hpp
index 36a2fbaa0..02fa07e4a 100644
--- a/src/xercesc/parsers/SAX2XMLReaderImpl.hpp
+++ b/src/xercesc/parsers/SAX2XMLReaderImpl.hpp
@@ -1775,6 +1775,7 @@ private :
     // -----------------------------------------------------------------------
     void initialize();
     void cleanUp();
+    void resetInProgress();
 
     // -----------------------------------------------------------------------
     //  Private data members
diff --git a/src/xercesc/parsers/SAXParser.cpp b/src/xercesc/parsers/SAXParser.cpp
index 27daf0254..464fcf860 100644
--- a/src/xercesc/parsers/SAXParser.cpp
+++ b/src/xercesc/parsers/SAXParser.cpp
@@ -281,6 +281,12 @@ XERCES_CPP_NAMESPACE_BEGIN
 // ---------------------------------------------------------------------------
 //  SAXParser: Constructors and Destructor
 // ---------------------------------------------------------------------------
+
+
+typedef JanitorMemFunCall<SAXParser>    CleanupType;
+typedef JanitorMemFunCall<SAXParser>    ResetInProgressType;
+
+
 SAXParser::SAXParser( XMLValidator* const   valToAdopt
                     , MemoryManager* const  manager
                     , XMLGrammarPool* const gramPool):
@@ -304,19 +310,22 @@ SAXParser::SAXParser( XMLValidator* const   valToAdopt
     , fGrammarPool(gramPool)
     , fElemQNameBuf(1023, manager)
 {
+    CleanupType cleanup(this, &SAXParser::cleanUp);
+
     try
     {
         initialize();
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 
@@ -763,19 +772,17 @@ void SAXParser::parse(const InputSource& source)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(source);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 }
@@ -786,19 +793,17 @@ void SAXParser::parse(const XMLCh* const systemId)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(systemId);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 }
@@ -809,19 +814,17 @@ void SAXParser::parse(const char* const systemId)
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
+
     try
     {
         fParseInProgress = true;
         fScanner->scanDocument(systemId);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 }
@@ -1510,20 +1513,18 @@ Grammar* SAXParser::loadGrammar(const char* const systemId,
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
+
     Grammar* grammar = 0;
     try
     {
         fParseInProgress = true;
         grammar = fScanner->loadGrammar(systemId, grammarType, toCache);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 
@@ -1538,20 +1539,18 @@ Grammar* SAXParser::loadGrammar(const XMLCh* const systemId,
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
+
     Grammar* grammar = 0;
     try
     {
         fParseInProgress = true;
         grammar = fScanner->loadGrammar(systemId, grammarType, toCache);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 
@@ -1566,26 +1565,29 @@ Grammar* SAXParser::loadGrammar(const InputSource& source,
     if (fParseInProgress)
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
+
     Grammar* grammar = 0;
     try
     {
         fParseInProgress = true;
         grammar = fScanner->loadGrammar(source, grammarType, toCache);
-        fParseInProgress = false;
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        fParseInProgress = false;
+        resetInProgress.release();
+
         throw;
     }
 
     return grammar;
 }
 
+void SAXParser::resetInProgress()
+{
+    fParseInProgress = false;
+}
+
 void SAXParser::resetCachedGrammarPool()
 {
     fGrammarResolver->resetCachedGrammar();
diff --git a/src/xercesc/parsers/SAXParser.hpp b/src/xercesc/parsers/SAXParser.hpp
index 660376560..9f7766237 100644
--- a/src/xercesc/parsers/SAXParser.hpp
+++ b/src/xercesc/parsers/SAXParser.hpp
@@ -2251,6 +2251,7 @@ private:
     // -----------------------------------------------------------------------
     void initialize();
     void cleanUp();
+    void resetInProgress();
 
     // -----------------------------------------------------------------------
     //  Private data members
diff --git a/src/xercesc/parsers/XercesDOMParser.cpp b/src/xercesc/parsers/XercesDOMParser.cpp
index 8e54f7cbd..86bb7b7cb 100644
--- a/src/xercesc/parsers/XercesDOMParser.cpp
+++ b/src/xercesc/parsers/XercesDOMParser.cpp
@@ -251,6 +251,8 @@ XercesDOMParser::resolveEntity(XMLResourceIdentifier* resourceIdentifier)
     return 0;
 }
 
+typedef JanitorMemFunCall<XercesDOMParser>  ResetParseType;
+
 // ---------------------------------------------------------------------------
 //  XercesDOMParser: Grammar preparsing methods
 // ---------------------------------------------------------------------------
@@ -262,26 +264,21 @@ Grammar* XercesDOMParser::loadGrammar(const char* const systemId,
     if (getParseInProgress())
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
+
     Grammar* grammar = 0;
+
     try
     {
         setParseInProgress(true);
         if (grammarType == Grammar::DTDGrammarType) 
             getScanner()->setDocTypeHandler(0);
         grammar = getScanner()->loadGrammar(systemId, grammarType, toCache);
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);        
-        setParseInProgress(false);
+        resetParse.release();
+
         throw;
     }
 
@@ -296,26 +293,21 @@ Grammar* XercesDOMParser::loadGrammar(const XMLCh* const systemId,
     if (getParseInProgress())
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
+    ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
+
     Grammar* grammar = 0;
+
     try
     {
         setParseInProgress(true);
         if (grammarType == Grammar::DTDGrammarType) 
             getScanner()->setDocTypeHandler(0);
         grammar = getScanner()->loadGrammar(systemId, grammarType, toCache);
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);        
-        setParseInProgress(false);
+        resetParse.release();
+
         throw;
     }
 
@@ -330,7 +322,10 @@ Grammar* XercesDOMParser::loadGrammar(const InputSource& source,
     if (getParseInProgress())
         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
 
-   Grammar* grammar = 0;
+    ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
+
+    Grammar* grammar = 0;
+
     try
     {
         setParseInProgress(true);
@@ -343,17 +338,22 @@ Grammar* XercesDOMParser::loadGrammar(const InputSource& source,
     }
     catch(const OutOfMemoryException&)
     {
+        resetParse.release();
+
         throw;
     }
-    catch(...)
+
+    return grammar;
+}
+
+void XercesDOMParser::resetParse()
+{
+    if (getScanner()->getDocTypeHandler() == 0)
     {
-        if (grammarType == Grammar::DTDGrammarType) 
-            getScanner()->setDocTypeHandler(this);
-        setParseInProgress(false);
-        throw;
+        getScanner()->setDocTypeHandler(this);
     }
 
-    return grammar;
+    setParseInProgress(false);
 }
 
 void XercesDOMParser::resetCachedGrammarPool()
diff --git a/src/xercesc/parsers/XercesDOMParser.hpp b/src/xercesc/parsers/XercesDOMParser.hpp
index a1ff5a639..8b9d8f9ac 100644
--- a/src/xercesc/parsers/XercesDOMParser.hpp
+++ b/src/xercesc/parsers/XercesDOMParser.hpp
@@ -639,6 +639,11 @@ public :
 
 
 private :
+    // -----------------------------------------------------------------------
+    //  Initialize/Cleanup methods
+    // -----------------------------------------------------------------------
+    void resetParse();
+
     // -----------------------------------------------------------------------
     //  Unimplemented constructors and operators
     // -----------------------------------------------------------------------
diff --git a/src/xercesc/util/Janitor.c b/src/xercesc/util/Janitor.c
index 496e9de43..e16ffd9f8 100644
--- a/src/xercesc/util/Janitor.c
+++ b/src/xercesc/util/Janitor.c
@@ -231,5 +231,38 @@ ArrayJanitor<T>::reset(T* p, MemoryManager* const manager)
     fMemoryManager = manager;
 }
 
-XERCES_CPP_NAMESPACE_END
 
+
+template <class T>
+JanitorMemFunCall<T>::JanitorMemFunCall(
+            T*      object,
+            MFPT    toCall) :
+    fObject(object),
+    fToCall(toCall)
+{
+}
+
+
+template <class T>
+JanitorMemFunCall<T>::~JanitorMemFunCall()
+{
+    if (fObject != 0 && fToCall != 0)
+    {
+        (fObject->*fToCall)();
+    }
+}
+
+
+// ---------------------------------------------------------------------------
+//  Janitor: Public, non-virtual methods
+// ---------------------------------------------------------------------------
+template <class T> void
+JanitorMemFunCall<T>::release()
+{
+    fObject = 0;
+    fToCall = 0;
+}
+
+
+
+XERCES_CPP_NAMESPACE_END
diff --git a/src/xercesc/util/Janitor.hpp b/src/xercesc/util/Janitor.hpp
index 054c75343..51ec27056 100644
--- a/src/xercesc/util/Janitor.hpp
+++ b/src/xercesc/util/Janitor.hpp
@@ -159,6 +159,50 @@ private :
     MemoryManager* fMemoryManager;
 };
 
+
+
+template <class T> class JanitorMemFunCall
+{
+public  :
+
+    typedef void (T::*MFPT) ();
+
+    // -----------------------------------------------------------------------
+    //  Constructors and Destructor
+    // -----------------------------------------------------------------------
+    JanitorMemFunCall(
+        T*      object,
+        MFPT    toCall);
+
+    ~JanitorMemFunCall();
+
+    // -----------------------------------------------------------------------
+    //  Public, non-virtual methods
+    // -----------------------------------------------------------------------
+	void release();
+
+private :
+    // -----------------------------------------------------------------------
+    //  Unimplemented constructors and operators
+    // -----------------------------------------------------------------------
+    JanitorMemFunCall();
+    JanitorMemFunCall(const JanitorMemFunCall<T>&);
+    JanitorMemFunCall<T>& operator=(const JanitorMemFunCall<T>&);
+
+    // -----------------------------------------------------------------------
+    //  Private data members
+    //
+    //  fObject
+    //      This is the pointer to the object for which we will call the
+    //      member function when this object is destroyed.
+    // -----------------------------------------------------------------------
+    T*      fObject;
+    MFPT    fToCall;
+};
+
+
+
+
 XERCES_CPP_NAMESPACE_END
 
 #if !defined(XERCES_TMPLSINC)
diff --git a/src/xercesc/util/QName.cpp b/src/xercesc/util/QName.cpp
index 15392cf1d..1766da87e 100644
--- a/src/xercesc/util/QName.cpp
+++ b/src/xercesc/util/QName.cpp
@@ -90,6 +90,7 @@
  *
  */
 
+#include <xercesc/util/Janitor.hpp>
 #include <xercesc/util/QName.hpp>
 #include <xercesc/util/OutOfMemoryException.hpp>
 
@@ -110,6 +111,8 @@ QName::QName(MemoryManager* const manager)
 {
 }
 
+typedef JanitorMemFunCall<QName>    CleanupType;
+
 QName::QName( const XMLCh* const   prefix
             , const XMLCh* const   localPart
             , const unsigned int   uriId
@@ -123,6 +126,8 @@ QName::QName( const XMLCh* const   prefix
 ,fRawName(0)
 ,fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &QName::cleanUp);
+
     try
     {
         //
@@ -133,12 +138,12 @@ QName::QName( const XMLCh* const   prefix
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 QName::QName( const XMLCh* const rawName
@@ -153,6 +158,8 @@ QName::QName( const XMLCh* const rawName
 ,fRawName(0)
 ,fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &QName::cleanUp);
+
     try
     {
         //
@@ -163,12 +170,12 @@ QName::QName( const XMLCh* const rawName
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 QName::~QName()
diff --git a/src/xercesc/util/XMLAbstractDoubleFloat.cpp b/src/xercesc/util/XMLAbstractDoubleFloat.cpp
index 5a4dc136b..1dd482090 100644
--- a/src/xercesc/util/XMLAbstractDoubleFloat.cpp
+++ b/src/xercesc/util/XMLAbstractDoubleFloat.cpp
@@ -612,10 +612,8 @@ XMLCh* XMLAbstractDoubleFloat::getCanonicalRepresentation(const XMLCh*         c
 
         janRetBuffer.release();
         return retBuffer;
-
-    } //try
-
-    catch (...)
+    }
+    catch (const NumberFormatException&)
     {
         return 0;
     }
diff --git a/src/xercesc/util/XMLBigDecimal.cpp b/src/xercesc/util/XMLBigDecimal.cpp
index 6331870c9..f89e9ac0e 100644
--- a/src/xercesc/util/XMLBigDecimal.cpp
+++ b/src/xercesc/util/XMLBigDecimal.cpp
@@ -137,6 +137,8 @@ XERCES_CPP_NAMESPACE_BEGIN
 //
 **/
 
+typedef JanitorMemFunCall<XMLBigDecimal>    CleanupType;
+
 XMLBigDecimal::XMLBigDecimal(const XMLCh* const strValue,
                              MemoryManager* const manager)
 : fSign(0)
@@ -150,6 +152,8 @@ XMLBigDecimal::XMLBigDecimal(const XMLCh* const strValue,
     if ((!strValue) || (!*strValue))
         ThrowXMLwithMemMgr(NumberFormatException, XMLExcepts::XMLNUM_emptyString, fMemoryManager);
 
+    CleanupType cleanup(this, &XMLBigDecimal::cleanUp);
+
     try
     {
         fRawDataLen = XMLString::stringLen(strValue);
@@ -164,13 +168,12 @@ XMLBigDecimal::XMLBigDecimal(const XMLCh* const strValue,
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 XMLBigDecimal::~XMLBigDecimal()
@@ -219,14 +222,19 @@ XMLCh* XMLBigDecimal::getCanonicalRepresentation(const XMLCh*         const rawD
                                                ,       MemoryManager* const memMgr)
 {
 
-    try 
-    {
-
     XMLCh* retBuf = (XMLCh*) memMgr->allocate( (XMLString::stringLen(rawData)+1) * sizeof(XMLCh));
     ArrayJanitor<XMLCh> janName(retBuf, memMgr);
     int   sign, totalDigits, fractDigits;
 
-    XMLBigDecimal::parseDecimal(rawData, retBuf, sign, totalDigits, fractDigits, memMgr);
+    try
+    {
+        parseDecimal(rawData, retBuf, sign, totalDigits, fractDigits, memMgr);
+    }
+    catch (const NumberFormatException&)
+    {
+        return 0;
+    }
+
 
     //Extra space reserved in case strLen is zero
     int    strLen = XMLString::stringLen(retBuf);
@@ -278,13 +286,6 @@ XMLCh* XMLBigDecimal::getCanonicalRepresentation(const XMLCh*         const rawD
     }
             
     return retBuffer;
-
-    }//try
-    catch (...)
-    {
-        return 0;
-    }
-
 }
 
 void  XMLBigDecimal::parseDecimal(const XMLCh* const toParse
diff --git a/src/xercesc/util/XMLBigInteger.cpp b/src/xercesc/util/XMLBigInteger.cpp
index d716e4140..0d195dab1 100644
--- a/src/xercesc/util/XMLBigInteger.cpp
+++ b/src/xercesc/util/XMLBigInteger.cpp
@@ -134,9 +134,8 @@ XMLCh* XMLBigInteger::getCanonicalRepresentation(const XMLCh*         const rawD
         jan.release();
         return retBuf;
 
-    }//
-
-    catch (...)
+    }
+    catch (const NumberFormatException&)
     {
         return 0;
     }
diff --git a/src/xercesc/util/XMLString.cpp b/src/xercesc/util/XMLString.cpp
index 30dca0906..c86bba2ee 100644
--- a/src/xercesc/util/XMLString.cpp
+++ b/src/xercesc/util/XMLString.cpp
@@ -43,6 +43,7 @@
 #include <xercesc/util/XMLUniDefs.hpp>
 #include <xercesc/util/XMLUni.hpp>
 #include <xercesc/util/XMLUri.hpp>
+#include <xercesc/util/XMLURL.hpp>
 #include <xercesc/internal/XMLReader.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
@@ -647,32 +648,37 @@ bool XMLString::isValidNOTATION(const XMLCh*         const name
         (colPos == nameLen - 1)  )     // <URI>':'
         return false;
 
-    // Examine URI
-    if (colPos > 0)
+
+    // Examine localpart
+    if (!XMLString::isValidNCName(&name[colPos+1]))
+    {
+        return false;
+    }
+    else if (colPos == 0)
+    {
+        return true;
+    }
+    else
     {
-        XMLCh* temp = (XMLCh*) &(name[colPos]);
-        *temp = 0;
+        // Examine URI
+        XMLCh* const temp =
+            (XMLCh*) manager->allocate((colPos + 1) * sizeof(XMLCh));
+        const ArrayJanitor<XMLCh> jan(temp, manager);
+
+        copyNString(temp, name, colPos);
+        temp[colPos] = 0;
 
         try
         {            
-            XMLUri  newURI(name, manager); // no relative uri support here
-            *temp = chColon;
+            XMLUri  newURI(temp, manager); // no relative uri support here
         }
-        catch(const OutOfMemoryException&)
+        catch (const MalformedURLException&)
         {
-            *temp = chColon;
             return false;
         }
-        catch (...)
-        {
-            *temp = chColon;
-            return false;
-        }
-    }
-
-    // Examine localpart
-    return XMLString::isValidNCName(&(name[colPos+1]));
 
+        return true;
+    }
 }
 
 
@@ -1662,7 +1668,7 @@ void XMLString::replaceWS(XMLCh* const toConvert
     (
         (strLen+1) * sizeof(XMLCh)
     );//new XMLCh[strLen+1];
-    XMLCh* retPtr = &(retBuf[0]);
+    XMLCh* retPtr = &retBuf[0];
     XMLCh* startPtr = toConvert;
 
     while ( *startPtr )
@@ -1762,7 +1768,7 @@ void XMLString::collapseWS(XMLCh* const toConvert
     (
         (endPtr - startPtr + 1) * sizeof(XMLCh)
     );//new XMLCh[endPtr - startPtr + 1];
-    XMLCh* retPtr = &(retBuf[0]);
+    XMLCh* retPtr = &retBuf[0];
     bool  inSpace = false;
     while (startPtr < endPtr)
     {
@@ -1809,7 +1815,7 @@ void XMLString::removeWS(XMLCh* const toConvert
     (
         (XMLString::stringLen(toConvert) + 1) * sizeof(XMLCh)
     );//new XMLCh[ XMLString::stringLen(toConvert) + 1];
-    XMLCh* retPtr = &(retBuf[0]);
+    XMLCh* retPtr = &retBuf[0];
     XMLCh* startPtr = toConvert;
 
     while (*startPtr)
diff --git a/src/xercesc/util/XMLStringTokenizer.cpp b/src/xercesc/util/XMLStringTokenizer.cpp
index 4f775fc0b..36c9d154a 100644
--- a/src/xercesc/util/XMLStringTokenizer.cpp
+++ b/src/xercesc/util/XMLStringTokenizer.cpp
@@ -54,6 +54,7 @@
 // ---------------------------------------------------------------------------
 //  Includes
 // ---------------------------------------------------------------------------
+#include <xercesc/util/Janitor.hpp>
 #include <xercesc/util/XMLStringTokenizer.hpp>
 #include <xercesc/util/XMLUniDefs.hpp>
 #include <xercesc/util/OutOfMemoryException.hpp>
@@ -71,6 +72,9 @@ const XMLCh fgDelimeters[] =
 // ---------------------------------------------------------------------------
 //  XMLStringTokenizer: Constructors and Destructor
 // ---------------------------------------------------------------------------
+
+typedef JanitorMemFunCall<XMLStringTokenizer>   CleanupType;
+
 XMLStringTokenizer::XMLStringTokenizer( const XMLCh* const srcStr
                                       , MemoryManager* const manager)
     : fOffset(0)
@@ -80,6 +84,8 @@ XMLStringTokenizer::XMLStringTokenizer( const XMLCh* const srcStr
     , fTokens(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLStringTokenizer::cleanUp);
+
 	try {
         if (fStringLen > 0) {
             fTokens = new (fMemoryManager) RefArrayVectorOf<XMLCh>(4, true, fMemoryManager);
@@ -87,11 +93,12 @@ XMLStringTokenizer::XMLStringTokenizer( const XMLCh* const srcStr
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...) {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 XMLStringTokenizer::XMLStringTokenizer(const XMLCh* const srcStr,
@@ -104,6 +111,8 @@ XMLStringTokenizer::XMLStringTokenizer(const XMLCh* const srcStr,
     , fTokens(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLStringTokenizer::cleanUp);
+
 	try {
 
         
@@ -114,11 +123,12 @@ XMLStringTokenizer::XMLStringTokenizer(const XMLCh* const srcStr,
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...) {
-        cleanUp();
-    }
+
+    cleanup.release();
 }
 
 XMLStringTokenizer::~XMLStringTokenizer()
diff --git a/src/xercesc/util/XMLURL.cpp b/src/xercesc/util/XMLURL.cpp
index e769a871e..b2b1acdc3 100644
--- a/src/xercesc/util/XMLURL.cpp
+++ b/src/xercesc/util/XMLURL.cpp
@@ -164,6 +164,8 @@ XMLURL::XMLURL(MemoryManager* const manager) :
 {
 }
 
+typedef JanitorMemFunCall<XMLURL>   CleanupType;
+
 XMLURL::XMLURL(const XMLCh* const    baseURL
              , const XMLCh* const    relativeURL
              , MemoryManager* const manager) :
@@ -180,19 +182,20 @@ XMLURL::XMLURL(const XMLCh* const    baseURL
     , fURLText(0)
     , fHasInvalidChar(false)
 {
+    CleanupType cleanup(this, &XMLURL::cleanUp);
+
 	try
 	{	
         setURL(baseURL, relativeURL);
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanup();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 XMLURL::XMLURL(const XMLCh* const  baseURL
@@ -211,6 +214,8 @@ XMLURL::XMLURL(const XMLCh* const  baseURL
     , fURLText(0)
     , fHasInvalidChar(false)
 {
+    CleanupType cleanup(this, &XMLURL::cleanUp);
+
     XMLCh* tmpRel = XMLString::transcode(relativeURL, fMemoryManager);
     ArrayJanitor<XMLCh> janRel(tmpRel, fMemoryManager);
 	try
@@ -219,13 +224,12 @@ XMLURL::XMLURL(const XMLCh* const  baseURL
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanup();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 XMLURL::XMLURL(const XMLURL&         baseURL
@@ -243,19 +247,20 @@ XMLURL::XMLURL(const XMLURL&         baseURL
     , fURLText(0)
     , fHasInvalidChar(false)
 {
+    CleanupType cleanup(this, &XMLURL::cleanUp);
+
 	try
 	{
 		setURL(baseURL, relativeURL);
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanup();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 XMLURL::XMLURL(const  XMLURL&        baseURL
@@ -273,6 +278,8 @@ XMLURL::XMLURL(const  XMLURL&        baseURL
     , fURLText(0)
     , fHasInvalidChar(false)
 {
+    CleanupType cleanup(this, &XMLURL::cleanUp);
+
     XMLCh* tmpRel = XMLString::transcode(relativeURL, fMemoryManager);
     ArrayJanitor<XMLCh> janRel(tmpRel, fMemoryManager);
 	try
@@ -281,14 +288,12 @@ XMLURL::XMLURL(const  XMLURL&        baseURL
 	}
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...)
-    {
-        cleanup();
+        cleanup.release();
+
         throw;
     }
 
+    cleanup.release();
 }
 
 XMLURL::XMLURL(const XMLCh* const urlText,
@@ -306,19 +311,20 @@ XMLURL::XMLURL(const XMLCh* const urlText,
     , fURLText(0)
     , fHasInvalidChar(false)
 {
+    CleanupType cleanup(this, &XMLURL::cleanUp);
+
 	try
 	{
 	    setURL(urlText);
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanup();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 XMLURL::XMLURL(const char* const urlText,
@@ -336,6 +342,8 @@ XMLURL::XMLURL(const char* const urlText,
     , fURLText(0)
     , fHasInvalidChar(false)
 {
+    CleanupType cleanup(this, &XMLURL::cleanUp);
+
     XMLCh* tmpText = XMLString::transcode(urlText, fMemoryManager);
     ArrayJanitor<XMLCh> janRel(tmpText, fMemoryManager);
 	try
@@ -344,13 +352,12 @@ XMLURL::XMLURL(const char* const urlText,
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanup();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 XMLURL::XMLURL(const XMLURL& toCopy) :
@@ -367,6 +374,8 @@ XMLURL::XMLURL(const XMLURL& toCopy) :
     , fURLText(0)
     , fHasInvalidChar(toCopy.fHasInvalidChar)
 {
+    CleanupType cleanup(this, &XMLURL::cleanUp);
+
     try
     {
         fFragment = XMLString::replicate(toCopy.fFragment, fMemoryManager);
@@ -379,18 +388,17 @@ XMLURL::XMLURL(const XMLURL& toCopy) :
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...)
-    {
-        cleanup();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 XMLURL::~XMLURL()
 {
-    cleanup();
+    cleanUp();
 }
 
 
@@ -403,7 +411,7 @@ XMLURL& XMLURL::operator=(const XMLURL& toAssign)
         return *this;
 
     // Clean up our stuff
-    cleanup();
+    cleanUp();
 
     // And copy his stuff
     fMemoryManager = toAssign.fMemoryManager;
@@ -473,14 +481,14 @@ void XMLURL::setURL(const XMLCh* const urlText)
     //
     //  Try to parse the URL.
     //
-    cleanup();
+    cleanUp();
     parse(urlText);
 }
 
 void XMLURL::setURL(const XMLCh* const    baseURL
                   , const XMLCh* const    relativeURL)
 {
-    cleanup();
+    cleanUp();
 
     // Parse our URL string
     parse(relativeURL);
@@ -496,7 +504,7 @@ void XMLURL::setURL(const XMLCh* const    baseURL
 			XMLURL basePart(baseURL, fMemoryManager);
 			if (!conglomerateWithBase(basePart, false))
 			{
-				cleanup();
+				cleanUp();
 				ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_RelativeBaseURL, fMemoryManager);
 			}
 		}
@@ -510,7 +518,7 @@ bool XMLURL::setURL(const XMLCh* const    baseURL
                   , const XMLCh* const    relativeURL
                   , XMLURL& xmlURL)
 {
-    cleanup();
+    cleanUp();
 
     // Parse our URL string
     if (parse(relativeURL, xmlURL))
@@ -535,7 +543,7 @@ bool XMLURL::setURL(const XMLCh* const    baseURL
 void XMLURL::setURL(const XMLURL&         baseURL
                   , const XMLCh* const    relativeURL)
 {
-    cleanup();
+    cleanUp();
 
 	// Parse our URL string
     parse(relativeURL);
@@ -765,7 +773,7 @@ void XMLURL::buildFullText()
 //  Just a central place to handle cleanup, since its done from a number
 //  of different spots.
 //
-void XMLURL::cleanup()
+void XMLURL::cleanUp()
 {
     fMemoryManager->deallocate(fFragment);//delete [] fFragment;
     fMemoryManager->deallocate(fHost);//delete [] fHost;
diff --git a/src/xercesc/util/XMLURL.hpp b/src/xercesc/util/XMLURL.hpp
index bdb24fe8e..2a8cdc537 100644
--- a/src/xercesc/util/XMLURL.hpp
+++ b/src/xercesc/util/XMLURL.hpp
@@ -155,7 +155,7 @@ private:
     //  Private helper methods
     // -----------------------------------------------------------------------
     void buildFullText();
-    void cleanup();
+    void cleanUp();
     bool conglomerateWithBase(const XMLURL& baseURL, bool useExceptions=true);
     void parse
     (
diff --git a/src/xercesc/util/XMLUri.cpp b/src/xercesc/util/XMLUri.cpp
index 4b9a543d3..2e8e33c82 100644
--- a/src/xercesc/util/XMLUri.cpp
+++ b/src/xercesc/util/XMLUri.cpp
@@ -236,6 +236,9 @@ static const XMLCh PATH_SEPARATORS[] =
 //  XMLUri: Constructors and Helper methods
 // ---------------------------------------------------------------------------
 // ctor# 2
+
+typedef JanitorMemFunCall<XMLUri>   CleanupType;
+
 XMLUri::XMLUri(const XMLCh* const uriSpec,
                MemoryManager* const manager)
 : fPort(-1)
@@ -249,18 +252,19 @@ XMLUri::XMLUri(const XMLCh* const uriSpec,
 , fURIText(0)
 , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLUri::cleanUp);
+
     try {
         initialize((XMLUri *)0, uriSpec);
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch (...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 // ctor# 7 relative ctor
@@ -278,18 +282,19 @@ XMLUri::XMLUri(const XMLUri* const      baseURI
 , fURIText(0)
 , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XMLUri::cleanUp);
+
     try {
         initialize(baseURI, uriSpec);
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch (...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 //Copy constructor
@@ -307,36 +312,39 @@ XMLUri::XMLUri(const XMLUri& toCopy)
 , fURIText(0)
 , fMemoryManager(toCopy.fMemoryManager)
 {
+    CleanupType cleanup(this, &XMLUri::cleanUp);
+
     try {
         initialize(toCopy);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        cleanUp();
+        cleanup.release();
+
         throw;
     }
 
+    cleanup.release();
 }
 
 XMLUri& XMLUri::operator=(const XMLUri& toAssign)
 {
     cleanUp();
+
+    CleanupType cleanup(this, &XMLUri::cleanUp);
+
     try {
         initialize(toAssign);
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch (...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
+
     return *this;
 }
 
@@ -788,18 +796,7 @@ void XMLUri::initializeAuthority(const XMLCh* const uriSpec)
 
         if (portStr && *portStr)
         {
-            try
-            {
-                port = XMLString::parseInt(portStr, fMemoryManager);
-            }
-            catch(const OutOfMemoryException&)
-            {
-                throw;
-            }
-            catch (...)
-            {
-                throw;
-            }
+            port = XMLString::parseInt(portStr, fMemoryManager);
         }
     } // if > 0
 
@@ -1144,18 +1141,7 @@ void XMLUri::setUserInfo(const XMLCh* const newUserInfo)
                 , fMemoryManager);
     }
 
-    try
-    {
-        isConformantUserInfo(newUserInfo, fMemoryManager);
-    }
-    catch(const OutOfMemoryException&)
-    {
-        throw;
-    }
-    catch (...)
-    {
-        throw;
-    }
+    isConformantUserInfo(newUserInfo, fMemoryManager);
 
     if (getUserInfo())
     {
@@ -2587,4 +2573,3 @@ XMLUri::XMLUri(MemoryManager* const manager)
 }
 
 XERCES_CPP_NAMESPACE_END
-
diff --git a/src/xercesc/util/regx/BMPattern.cpp b/src/xercesc/util/regx/BMPattern.cpp
index 61c10f6fa..ed2483f83 100644
--- a/src/xercesc/util/regx/BMPattern.cpp
+++ b/src/xercesc/util/regx/BMPattern.cpp
@@ -56,6 +56,9 @@ XERCES_CPP_NAMESPACE_BEGIN
 // ---------------------------------------------------------------------------
 //  BMPattern: Constructors
 // ---------------------------------------------------------------------------
+
+typedef JanitorMemFunCall<BMPattern>    CleanupType;
+
 BMPattern::BMPattern( const XMLCh*         const pattern
                     ,       bool                 ignoreCase
                     ,       MemoryManager* const manager) :
@@ -67,19 +70,20 @@ BMPattern::BMPattern( const XMLCh*         const pattern
     , fUppercasePattern(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &BMPattern::cleanUp);
+
 	try {
         fPattern = XMLString::replicate(pattern, fMemoryManager);
 		initialize();
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...) {
 
-		cleanUp();
-		throw;
-	}
+    cleanup.release();
 }
 
 BMPattern::BMPattern( const XMLCh*         const pattern
@@ -94,19 +98,20 @@ BMPattern::BMPattern( const XMLCh*         const pattern
     , fUppercasePattern(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &BMPattern::cleanUp);
+
 	try {
         fPattern = XMLString::replicate(pattern, fMemoryManager);
 		initialize();
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...) {
 
-		cleanUp();
-		throw;
-	}
+    cleanup.release();
 }
 
 BMPattern::~BMPattern() {
diff --git a/src/xercesc/util/regx/RangeTokenMap.cpp b/src/xercesc/util/regx/RangeTokenMap.cpp
index ed290f873..c629bb401 100644
--- a/src/xercesc/util/regx/RangeTokenMap.cpp
+++ b/src/xercesc/util/regx/RangeTokenMap.cpp
@@ -100,6 +100,7 @@
 #include <xercesc/util/XMLRegisterCleanup.hpp>
 #include <xercesc/util/StringPool.hpp>
 #include <xercesc/util/XMLInitializer.hpp>
+#include <xercesc/util/OutOfMemoryException.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
 
@@ -170,6 +171,9 @@ RangeTokenElemMap::~RangeTokenElemMap()
 // ---------------------------------------------------------------------------
 //  RangeTokenMap: Constructors and Destructor
 // ---------------------------------------------------------------------------
+
+typedef JanitorMemFunCall<RangeTokenMap>    CleanupType;
+
 RangeTokenMap::RangeTokenMap(MemoryManager* manager) :
     fTokenRegistry(0)
     , fRangeMap(0)
@@ -177,6 +181,8 @@ RangeTokenMap::RangeTokenMap(MemoryManager* manager) :
     , fTokenFactory(0)
     , fMutex(manager)
 {
+    CleanupType cleanup(this, &RangeTokenMap::cleanUp);
+
     try {
         fTokenRegistry = new (manager) RefHashTableOf<RangeTokenElemMap>(109, manager);
         fRangeMap = new (manager) RefHashTableOf<RangeFactory>(29, manager);
@@ -184,25 +190,19 @@ RangeTokenMap::RangeTokenMap(MemoryManager* manager) :
         fTokenFactory = new (manager) TokenFactory(manager);
         initializeRegistry();
     }
-    catch(...) {
-        cleanUp();
+    catch(const OutOfMemoryException&)
+    {
+        cleanup.release();
+
         throw;
     }
+
+    cleanup.release();
 }
 
 RangeTokenMap::~RangeTokenMap() {
 
-    delete fTokenRegistry;
-    fTokenRegistry = 0;
-
-    delete fRangeMap;
-    fRangeMap = 0;
-
-    delete fCategories;
-    fCategories = 0;
-
-    delete fTokenFactory;
-    fTokenFactory = 0;
+    cleanUp();
 }
 
 // ---------------------------------------------------------------------------
diff --git a/src/xercesc/util/regx/RegularExpression.cpp b/src/xercesc/util/regx/RegularExpression.cpp
index 96cb73dde..379bfd051 100644
--- a/src/xercesc/util/regx/RegularExpression.cpp
+++ b/src/xercesc/util/regx/RegularExpression.cpp
@@ -256,6 +256,9 @@ bool RegularExpression::Context::nextCh(XMLInt32& ch, int& offset,
 // ---------------------------------------------------------------------------
 //  RegularExpression: Constructors and Destructors
 // ---------------------------------------------------------------------------
+
+typedef JanitorMemFunCall<RegularExpression>    CleanupType;
+
 RegularExpression::RegularExpression(const char* const pattern,
                                      MemoryManager* const manager)
 	:fHasBackReferences(false),
@@ -274,6 +277,8 @@ RegularExpression::RegularExpression(const char* const pattern,
      fTokenFactory(0),
      fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &RegularExpression::cleanUp);
+
 	try {
 
 		XMLCh* tmpBuf = XMLString::transcode(pattern, fMemoryManager);
@@ -282,13 +287,12 @@ RegularExpression::RegularExpression(const char* const pattern,
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch (...) {
 
-		cleanUp();
-		throw;
-	}
+    cleanup.release();
 }
 
 RegularExpression::RegularExpression(const char* const pattern,
@@ -310,6 +314,8 @@ RegularExpression::RegularExpression(const char* const pattern,
      fTokenFactory(0),
      fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &RegularExpression::cleanUp);
+
 	try {
 
 		XMLCh* tmpBuf = XMLString::transcode(pattern, fMemoryManager);
@@ -320,13 +326,12 @@ RegularExpression::RegularExpression(const char* const pattern,
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch (...) {
 
-		cleanUp();
-		throw;
-	}
+    cleanup.release();
 }
 
 
@@ -348,19 +353,20 @@ RegularExpression::RegularExpression(const XMLCh* const pattern,
      fTokenFactory(0),
      fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &RegularExpression::cleanUp);
+
 	try {
 
 		setPattern(pattern);
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch (...) {
 
-		cleanUp();
-		throw;
-	}
+    cleanup.release();
 }
 
 RegularExpression::RegularExpression(const XMLCh* const pattern,
@@ -382,19 +388,20 @@ RegularExpression::RegularExpression(const XMLCh* const pattern,
      fTokenFactory(0),
      fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &RegularExpression::cleanUp);
+
 	try {
 
 		setPattern(pattern, options);
 	}
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch (...) {
 
-		cleanUp();
-		throw;
-	}
+    cleanup.release();
 }
 
 RegularExpression::~RegularExpression() {
@@ -1567,7 +1574,6 @@ const XMLCh* RegularExpression::subInExp(const XMLCh* const repString,
 }
 
 
-
 /*
  * Prepares for matching. This method is called during construction.
  */
@@ -1655,7 +1661,7 @@ unsigned short RegularExpression::getCharType(const XMLCh ch) {
 
 		if (isSet(fOptions, USE_UNICODE_CATEGORY)) {
 
-            if (fWordRange == 0) {
+			if (fWordRange == 0) {
 
 				fWordRange = fTokenFactory->getRange(fgUniIsWord);
 				if (fWordRange == 0)
diff --git a/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp b/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp
index 84feb51a7..874cc8fa4 100644
--- a/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp
@@ -118,18 +118,7 @@ AnyURIDatatypeValidator::AnyURIDatatypeValidator(
                         , MemoryManager* const manager)
 :AbstractStringValidator(baseValidator, facets, finalSet, DatatypeValidator::AnyURI, manager)
 {
-    try
-    {
-        init(enums, manager);
-    }
-    catch(const OutOfMemoryException&)
-    {
-        throw;
-    }
-    catch (...)
-    {        
-        throw;
-    }
+    init(enums, manager);
 }
 
 DatatypeValidator* AnyURIDatatypeValidator::newInstance(
diff --git a/src/xercesc/validators/datatype/DateDatatypeValidator.cpp b/src/xercesc/validators/datatype/DateDatatypeValidator.cpp
index e33429091..5efdd8e9a 100644
--- a/src/xercesc/validators/datatype/DateDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/DateDatatypeValidator.cpp
@@ -109,6 +109,7 @@ DatatypeValidator* DateDatatypeValidator::newInstance
 XMLDateTime* DateDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -116,15 +117,12 @@ XMLDateTime* DateDatatypeValidator::parse(const XMLCh* const content, MemoryMana
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void DateDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/DateTimeDatatypeValidator.cpp b/src/xercesc/validators/datatype/DateTimeDatatypeValidator.cpp
index 70566e676..3d38321ff 100644
--- a/src/xercesc/validators/datatype/DateTimeDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/DateTimeDatatypeValidator.cpp
@@ -119,6 +119,7 @@ DatatypeValidator* DateTimeDatatypeValidator::newInstance
 XMLDateTime* DateTimeDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -126,15 +127,12 @@ XMLDateTime* DateTimeDatatypeValidator::parse(const XMLCh* const content, Memory
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void DateTimeDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/DayDatatypeValidator.cpp b/src/xercesc/validators/datatype/DayDatatypeValidator.cpp
index 1e82d5a24..77e2d865b 100644
--- a/src/xercesc/validators/datatype/DayDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/DayDatatypeValidator.cpp
@@ -109,6 +109,7 @@ DatatypeValidator* DayDatatypeValidator::newInstance
 XMLDateTime* DayDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -116,15 +117,12 @@ XMLDateTime* DayDatatypeValidator::parse(const XMLCh* const content, MemoryManag
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void DayDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/DurationDatatypeValidator.cpp b/src/xercesc/validators/datatype/DurationDatatypeValidator.cpp
index eb1fb7c60..f33350d14 100644
--- a/src/xercesc/validators/datatype/DurationDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/DurationDatatypeValidator.cpp
@@ -112,6 +112,7 @@ DatatypeValidator* DurationDatatypeValidator::newInstance
 XMLDateTime* DurationDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -119,15 +120,12 @@ XMLDateTime* DurationDatatypeValidator::parse(const XMLCh* const content, Memory
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void DurationDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/ListDatatypeValidator.cpp b/src/xercesc/validators/datatype/ListDatatypeValidator.cpp
index 8e8364e29..f4d520223 100644
--- a/src/xercesc/validators/datatype/ListDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/ListDatatypeValidator.cpp
@@ -430,6 +430,7 @@ void ListDatatypeValidator::inspectFacetBase(MemoryManager* const manager)
                 {
                     // ask the itemType for a complete check
                     BaseRefVectorOf<XMLCh>* tempList = XMLString::tokenizeString(getEnumeration()->elementAt(i), manager);
+                    Janitor<BaseRefVectorOf<XMLCh> >    jan(tempList);
                     int tokenNumber = tempList->size();
 
                     try
@@ -439,15 +440,10 @@ void ListDatatypeValidator::inspectFacetBase(MemoryManager* const manager)
                     }
                     catch(const OutOfMemoryException&)
                     {
+                        jan.release();
+
                         throw;
                     }
-                    catch (...)
-                    {
-                        delete tempList;
-                        throw;
-                    }
-
-                    delete tempList;
 
                     // enum shall pass this->checkContent() as well.
                     checkContent(getEnumeration()->elementAt(i), (ValidationContext*)0, false, manager);
diff --git a/src/xercesc/validators/datatype/MonthDatatypeValidator.cpp b/src/xercesc/validators/datatype/MonthDatatypeValidator.cpp
index a217272f1..3344ad72d 100644
--- a/src/xercesc/validators/datatype/MonthDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/MonthDatatypeValidator.cpp
@@ -109,6 +109,7 @@ DatatypeValidator* MonthDatatypeValidator::newInstance
 XMLDateTime* MonthDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -116,15 +117,12 @@ XMLDateTime* MonthDatatypeValidator::parse(const XMLCh* const content, MemoryMan
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void MonthDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/MonthDayDatatypeValidator.cpp b/src/xercesc/validators/datatype/MonthDayDatatypeValidator.cpp
index 72cd7e5cf..43ff74838 100644
--- a/src/xercesc/validators/datatype/MonthDayDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/MonthDayDatatypeValidator.cpp
@@ -109,6 +109,7 @@ DatatypeValidator* MonthDayDatatypeValidator::newInstance
 XMLDateTime* MonthDayDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -116,15 +117,12 @@ XMLDateTime* MonthDayDatatypeValidator::parse(const XMLCh* const content, Memory
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void MonthDayDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/TimeDatatypeValidator.cpp b/src/xercesc/validators/datatype/TimeDatatypeValidator.cpp
index 876c08201..9e44602ee 100644
--- a/src/xercesc/validators/datatype/TimeDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/TimeDatatypeValidator.cpp
@@ -122,6 +122,7 @@ DatatypeValidator* TimeDatatypeValidator::newInstance
 XMLDateTime* TimeDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -129,15 +130,12 @@ XMLDateTime* TimeDatatypeValidator::parse(const XMLCh* const content, MemoryMana
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void TimeDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/UnionDatatypeValidator.cpp b/src/xercesc/validators/datatype/UnionDatatypeValidator.cpp
index 19baa2573..0125fb3b1 100644
--- a/src/xercesc/validators/datatype/UnionDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/UnionDatatypeValidator.cpp
@@ -173,6 +173,8 @@ UnionDatatypeValidator::UnionDatatypeValidator(
     fMemberTypeValidators = memberTypeValidators;
 }
 
+typedef JanitorMemFunCall<UnionDatatypeValidator>   CleanupType;
+
 UnionDatatypeValidator::UnionDatatypeValidator(
                           DatatypeValidator*            const baseValidator
                         , RefHashTableOf<KVStringPair>* const facets
@@ -210,19 +212,22 @@ UnionDatatypeValidator::UnionDatatypeValidator(
                 , manager);
     }
 
+    CleanupType cleanup(this, &UnionDatatypeValidator::cleanUp);
+
     try
     {
         init(baseValidator, facets, enums, manager);
     }
     catch(const OutOfMemoryException&)
     {
+        // Don't cleanup when out of memory, since executing the
+        // code can cause problems.
+        cleanup.release();
+
         throw;
     }
-    catch (...)
-    {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 void UnionDatatypeValidator::init(DatatypeValidator*            const baseValidator
diff --git a/src/xercesc/validators/datatype/YearDatatypeValidator.cpp b/src/xercesc/validators/datatype/YearDatatypeValidator.cpp
index b69f82e93..0fde148a4 100644
--- a/src/xercesc/validators/datatype/YearDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/YearDatatypeValidator.cpp
@@ -109,6 +109,7 @@ DatatypeValidator* YearDatatypeValidator::newInstance
 XMLDateTime* YearDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -116,15 +117,12 @@ XMLDateTime* YearDatatypeValidator::parse(const XMLCh* const content, MemoryMana
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void YearDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/datatype/YearMonthDatatypeValidator.cpp b/src/xercesc/validators/datatype/YearMonthDatatypeValidator.cpp
index de1004627..512f086b5 100644
--- a/src/xercesc/validators/datatype/YearMonthDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/YearMonthDatatypeValidator.cpp
@@ -109,6 +109,7 @@ DatatypeValidator* YearMonthDatatypeValidator::newInstance
 XMLDateTime* YearMonthDatatypeValidator::parse(const XMLCh* const content, MemoryManager* const manager)
 {
     XMLDateTime *pRetDate = new (manager) XMLDateTime(content, manager);
+    Janitor<XMLDateTime> jan(pRetDate);
 
     try
     {
@@ -116,15 +117,12 @@ XMLDateTime* YearMonthDatatypeValidator::parse(const XMLCh* const content, Memor
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch (...)
-    {
-        delete pRetDate;
+        jan.release();
+
         throw;
     }
 
-    return pRetDate;
+    return jan.release();
 }
 
 void YearMonthDatatypeValidator::parse(XMLDateTime* const pDate)
diff --git a/src/xercesc/validators/schema/SchemaGrammar.cpp b/src/xercesc/validators/schema/SchemaGrammar.cpp
index 2c14577f4..edf6a7d5a 100644
--- a/src/xercesc/validators/schema/SchemaGrammar.cpp
+++ b/src/xercesc/validators/schema/SchemaGrammar.cpp
@@ -138,6 +138,8 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+typedef JanitorMemFunCall<SchemaGrammar>    CleanupType;
+
 // ---------------------------------------------------------------------------
 //  SchemaGrammar: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -160,6 +162,8 @@ SchemaGrammar::SchemaGrammar(MemoryManager* const manager) :
     , fValidated(false)
     , fDatatypeRegistry(manager)
 {
+    CleanupType cleanup(this, &SchemaGrammar::cleanUp);
+
     //
     //  Init all the pool members.
     //
@@ -195,13 +199,12 @@ SchemaGrammar::SchemaGrammar(MemoryManager* const manager) :
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...) {
+        cleanup.release();
 
-        cleanUp();
         throw;
     }
+
+    cleanup.release();
 }
 
 SchemaGrammar::~SchemaGrammar()
diff --git a/src/xercesc/validators/schema/TraverseSchema.cpp b/src/xercesc/validators/schema/TraverseSchema.cpp
index 19502d956..1069dac5d 100644
--- a/src/xercesc/validators/schema/TraverseSchema.cpp
+++ b/src/xercesc/validators/schema/TraverseSchema.cpp
@@ -158,6 +158,8 @@ enum {
     ENUM_ELT_SIZE
 };
 
+typedef JanitorMemFunCall<TraverseSchema>   CleanupType;
+
 // ---------------------------------------------------------------------------
 //  TraverseSchema: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -219,6 +221,7 @@ TraverseSchema::TraverseSchema( DOMElement* const    schemaRoot
     , fAnnotation(0)
     , fAttributeCheck(manager)
 {
+    CleanupType cleanup(this, &TraverseSchema::cleanUp);
 
     try {
 
@@ -232,13 +235,12 @@ TraverseSchema::TraverseSchema( DOMElement* const    schemaRoot
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...) {
+        cleanup.release();
 
-        cleanUp();
         throw;
     }
+
+    cleanup.release();
 }
 
 
diff --git a/src/xercesc/validators/schema/identity/FieldValueMap.cpp b/src/xercesc/validators/schema/identity/FieldValueMap.cpp
index 8e3328abc..5b3beb558 100644
--- a/src/xercesc/validators/schema/identity/FieldValueMap.cpp
+++ b/src/xercesc/validators/schema/identity/FieldValueMap.cpp
@@ -52,10 +52,13 @@
 //  Includes
 // ---------------------------------------------------------------------------
 #include <xercesc/validators/schema/identity/FieldValueMap.hpp>
+#include <xercesc/util/Janitor.hpp>
 #include <xercesc/util/OutOfMemoryException.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+typedef JanitorMemFunCall<FieldValueMap>    CleanupType;
+
 // ---------------------------------------------------------------------------
 //  FieldValueMap: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -74,34 +77,41 @@ FieldValueMap::FieldValueMap(const FieldValueMap& other)
     , fValues(0)
     , fMemoryManager(other.fMemoryManager)
 {
-    try {
-        if (other.fFields) {
+    if (other.fFields) {
+        CleanupType cleanup(this, &FieldValueMap::cleanUp);
 
-            unsigned int valuesSize = other.fValues->size();
+        try {
 
-            fFields = new (fMemoryManager) ValueVectorOf<IC_Field*>(*(other.fFields));
-            fValidators = new (fMemoryManager) ValueVectorOf<DatatypeValidator*>(*(other.fValidators));
-            fValues = new (fMemoryManager) RefArrayVectorOf<XMLCh>(other.fFields->curCapacity(), true, fMemoryManager);
+                unsigned int valuesSize = other.fValues->size();
 
-            for (unsigned int i=0; i<valuesSize; i++) {
-                fValues->addElement(XMLString::replicate(other.fValues->elementAt(i), fMemoryManager));
-            }
+                fFields = new (fMemoryManager) ValueVectorOf<IC_Field*>(*(other.fFields));
+                fValidators = new (fMemoryManager) ValueVectorOf<DatatypeValidator*>(*(other.fValidators));
+                fValues = new (fMemoryManager) RefArrayVectorOf<XMLCh>(other.fFields->curCapacity(), true, fMemoryManager);
+
+                for (unsigned int i=0; i<valuesSize; i++) {
+                    fValues->addElement(XMLString::replicate(other.fValues->elementAt(i), fMemoryManager));
+                }
+        }
+        catch(const OutOfMemoryException&)
+        {
+            cleanup.release();
+
+            throw;
         }
-    }
-    catch(const OutOfMemoryException&)
-    {
-        throw;
-    }
-    catch(...) {
 
-        delete fFields;
-        delete fValidators;
-        delete fValues;
-        throw;
+        cleanup.release();
     }
 }
 
 FieldValueMap::~FieldValueMap()
+{
+    cleanUp();
+}
+
+// ---------------------------------------------------------------------------
+//  FieldValueMap: Private helper methods.
+// ---------------------------------------------------------------------------
+void FieldValueMap::cleanUp()
 {
     delete fFields;
     delete fValidators;
diff --git a/src/xercesc/validators/schema/identity/FieldValueMap.hpp b/src/xercesc/validators/schema/identity/FieldValueMap.hpp
index fb0ae3714..72cf1f607 100644
--- a/src/xercesc/validators/schema/identity/FieldValueMap.hpp
+++ b/src/xercesc/validators/schema/identity/FieldValueMap.hpp
@@ -74,6 +74,11 @@ public:
     int indexOf(const IC_Field* const key) const;
 
 private:
+    // -----------------------------------------------------------------------
+    //  Private helper methods
+    // -----------------------------------------------------------------------
+    void cleanUp();
+
     // -----------------------------------------------------------------------
     //  Unimplemented operators
     // -----------------------------------------------------------------------
diff --git a/src/xercesc/validators/schema/identity/IdentityConstraint.cpp b/src/xercesc/validators/schema/identity/IdentityConstraint.cpp
index e29b14d8d..ccbe68801 100644
--- a/src/xercesc/validators/schema/identity/IdentityConstraint.cpp
+++ b/src/xercesc/validators/schema/identity/IdentityConstraint.cpp
@@ -77,6 +77,8 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+typedef JanitorMemFunCall<IdentityConstraint>   CleanupType;
+
 // ---------------------------------------------------------------------------
 //  IdentityConstraint: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -90,19 +92,20 @@ IdentityConstraint::IdentityConstraint(const XMLCh* const identityConstraintName
     , fMemoryManager(manager)
     , fNamespaceURI(-1)
 {
+    CleanupType cleanup(this, &IdentityConstraint::cleanUp);
+
     try {
         fIdentityConstraintName = XMLString::replicate(identityConstraintName, fMemoryManager);
         fElemName = XMLString::replicate(elemName, fMemoryManager);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...) {
+        cleanup.release();
 
-        cleanUp();
         throw;
     }
+
+    cleanup.release();
 }
 
 
diff --git a/src/xercesc/validators/schema/identity/IdentityConstraintHandler.cpp b/src/xercesc/validators/schema/identity/IdentityConstraintHandler.cpp
index e7390c3cc..933c98447 100644
--- a/src/xercesc/validators/schema/identity/IdentityConstraintHandler.cpp
+++ b/src/xercesc/validators/schema/identity/IdentityConstraintHandler.cpp
@@ -41,6 +41,8 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+typedef JanitorMemFunCall<IdentityConstraintHandler>    CleanupType;
+
 // ---------------------------------------------------------------------------
 //  IdentityConstraintHandler: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -52,6 +54,7 @@ IdentityConstraintHandler::IdentityConstraintHandler(XMLScanner*    const scanne
 , fValueStoreCache(0)
 , fFieldActivator(0)
 {
+    CleanupType cleanup(this, &IdentityConstraintHandler::cleanUp);
 
     try {
 
@@ -63,12 +66,12 @@ IdentityConstraintHandler::IdentityConstraintHandler(XMLScanner*    const scanne
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...) {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 IdentityConstraintHandler::~IdentityConstraintHandler()
diff --git a/src/xercesc/validators/schema/identity/ValueStoreCache.cpp b/src/xercesc/validators/schema/identity/ValueStoreCache.cpp
index e96047b03..913ce08d1 100644
--- a/src/xercesc/validators/schema/identity/ValueStoreCache.cpp
+++ b/src/xercesc/validators/schema/identity/ValueStoreCache.cpp
@@ -65,6 +65,8 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+typedef JanitorMemFunCall<ValueStoreCache>    CleanupType;
+
 // ---------------------------------------------------------------------------
 //  ValueStoreCache: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -76,17 +78,19 @@ ValueStoreCache::ValueStoreCache(MemoryManager* const manager)
     , fScanner(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &ValueStoreCache::cleanUp);
+
     try {
         init();
     }
     catch(const OutOfMemoryException&)
     {
+        cleanup.release();
+
         throw;
     }
-    catch(...) {
-        cleanUp();
-        throw;
-    }
+
+    cleanup.release();
 }
 
 
diff --git a/src/xercesc/validators/schema/identity/XPathMatcher.cpp b/src/xercesc/validators/schema/identity/XPathMatcher.cpp
index a8373b5ac..7a41087ea 100644
--- a/src/xercesc/validators/schema/identity/XPathMatcher.cpp
+++ b/src/xercesc/validators/schema/identity/XPathMatcher.cpp
@@ -73,6 +73,8 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+typedef JanitorMemFunCall<XPathMatcher>     CleanupType;
+
 // ---------------------------------------------------------------------------
 //  XPathMatcher: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -87,18 +89,19 @@ XPathMatcher::XPathMatcher( XercesXPath* const xpath
     , fIdentityConstraint(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XPathMatcher::cleanUp);
+
     try {
         init(xpath);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...) {
+        cleanup.release();
 
-        cleanUp();
         throw;
     }
+
+    cleanup.release();
 }
 
 
@@ -114,18 +117,19 @@ XPathMatcher::XPathMatcher(XercesXPath* const xpath,
     , fIdentityConstraint(ic)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XPathMatcher::cleanUp);
+
     try {
         init(xpath);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...) {
+        cleanup.release();
 
-        cleanUp();
         throw;
     }
+
+    cleanup.release();
 }
 
 
diff --git a/src/xercesc/validators/schema/identity/XPathMatcherStack.cpp b/src/xercesc/validators/schema/identity/XPathMatcherStack.cpp
index c19f92b12..472d67170 100644
--- a/src/xercesc/validators/schema/identity/XPathMatcherStack.cpp
+++ b/src/xercesc/validators/schema/identity/XPathMatcherStack.cpp
@@ -43,34 +43,47 @@
 //  Includes
 // ---------------------------------------------------------------------------
 #include <xercesc/validators/schema/identity/XPathMatcherStack.hpp>
+#include <xercesc/util/Janitor.hpp>
 #include <xercesc/util/OutOfMemoryException.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+typedef JanitorMemFunCall<XPathMatcherStack>    CleanupType;
+
 // ---------------------------------------------------------------------------
 //  XPathMatherStack: Constructors and Destructor
 // ---------------------------------------------------------------------------
 XPathMatcherStack::XPathMatcherStack(MemoryManager* const manager)
     : fMatchersCount(0)
-    , fContextStack(new (manager) ValueStackOf<int>(8, manager))
+    , fContextStack(0)
     , fMatchers(0)
 {
+    CleanupType cleanup(this, &XPathMatcherStack::cleanUp);
+
     try {
+        fContextStack = new (manager) ValueStackOf<int>(8, manager);
         fMatchers = new (manager) RefVectorOf<XPathMatcher>(8, true, manager);
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...) {
+        cleanup.release();
 
-        delete fContextStack;
         throw;
     }
+
+    cleanup.release();
 }
 
 XPathMatcherStack::~XPathMatcherStack() {
 
+    cleanUp();
+}
+
+// ---------------------------------------------------------------------------
+//  XPathMatcherStack: Private helper methods.
+// ---------------------------------------------------------------------------
+void XPathMatcherStack::cleanUp()
+{
     delete fContextStack;
     delete fMatchers;
 }
diff --git a/src/xercesc/validators/schema/identity/XPathMatcherStack.hpp b/src/xercesc/validators/schema/identity/XPathMatcherStack.hpp
index 68b633c68..a019a7bb4 100644
--- a/src/xercesc/validators/schema/identity/XPathMatcherStack.hpp
+++ b/src/xercesc/validators/schema/identity/XPathMatcherStack.hpp
@@ -62,6 +62,11 @@ public:
     void clear();
 
 private:
+    // -----------------------------------------------------------------------
+    //  Private helper methods
+    // -----------------------------------------------------------------------
+    void cleanUp();
+
     // -----------------------------------------------------------------------
     //  Unimplemented contstructors and operators
     // -----------------------------------------------------------------------
diff --git a/src/xercesc/validators/schema/identity/XercesXPath.cpp b/src/xercesc/validators/schema/identity/XercesXPath.cpp
index 04015d424..bf03ace0b 100644
--- a/src/xercesc/validators/schema/identity/XercesXPath.cpp
+++ b/src/xercesc/validators/schema/identity/XercesXPath.cpp
@@ -346,6 +346,8 @@ XercesLocationPath::XercesLocationPath(MemoryManager* const)
 {
 }
 
+typedef JanitorMemFunCall<XercesXPath>  CleanupType;
+
 // ---------------------------------------------------------------------------
 //  XercesPath: Constructors and Destructor
 // ---------------------------------------------------------------------------
@@ -360,6 +362,8 @@ XercesXPath::XercesXPath(const XMLCh* const xpathExpr,
     , fLocationPaths(0)
     , fMemoryManager(manager)
 {
+    CleanupType cleanup(this, &XercesXPath::cleanUp);
+
     try
     {
         fExpression = XMLString::replicate(xpathExpr, fMemoryManager);
@@ -371,13 +375,12 @@ XercesXPath::XercesXPath(const XMLCh* const xpathExpr,
     }
     catch(const OutOfMemoryException&)
     {
-        throw;
-    }
-    catch(...) {
+        cleanup.release();
 
-        cleanUp();
         throw;
     }
+
+    cleanup.release();
 }
 
 XercesXPath::~XercesXPath() {
-- 
GitLab